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
30 use fft_oct_m
31 use global_oct_m
32 use grid_oct_m
33 use io_oct_m
36 use ions_oct_m
37 use, intrinsic :: iso_fortran_env
42 use loct_oct_m
44 use mesh_oct_m
47 use mpi_oct_m
51 use parser_oct_m
56 use space_oct_m
57 use string_oct_m
62 use types_oct_m
63 use unit_oct_m
65 use utils_oct_m
68
69 implicit none
70
71 integer :: w90_what, w90_mode, w90_what_default
72
73 integer :: ierr
74 integer :: dim, idim
75 integer :: ii, nik, iter, nst
76
77 type(restart_t) :: restart
78 type(electrons_t), pointer :: sys
79 logical :: w90_spinors, scdm_proj, w90_scdm
80 integer :: w90_nntot, w90_num_bands, w90_num_kpts ! w90 input parameters
81 integer, allocatable :: w90_nnk_list(:,:) !
82 character(len=80) :: w90_prefix ! w90 input file prefix
83 integer :: w90_num_wann ! input paramter
84 real(real64), allocatable :: w90_proj_centers(:,:) ! projections centers
85 integer, allocatable :: w90_proj_lmr(:,:) ! definitions for real valued Y_lm*R_r
86 integer :: w90_nproj ! number of such projections
87 integer, allocatable :: w90_spin_proj_component(:) ! up/down flag
88 real(real64), allocatable :: w90_spin_proj_axis(:,:) ! spin axis (not implemented)
89 integer :: w90_num_exclude
90 logical, allocatable :: exclude_list(:) ! list of excluded bands
91 integer, allocatable :: band_index(:) ! band index after exclusion
92 logical :: read_td_states
93 integer :: w90_spin_channel
94
95 ! scdm variables
96 integer, allocatable :: jpvt(:)
97 complex(real64), allocatable :: uk(:,:,:) ! SCDM-Wannier gauge matrices U(k)
98 complex(real64), allocatable :: psi(:,:)
99 complex(real64), allocatable :: chi(:,:), chi_diag(:,:),chi2(:,:)
100 real(real64), allocatable :: chi_eigenval(:), occ_temp(:)
101 real(real64) :: scdm_mu, scdm_sigma, smear, kvec(3), factor(3)
102 integer :: ist, jst, ik, idir
103
104 integer(int64) :: how
105
106 call global_init()
107 call parser_init()
108
109 call messages_init()
110 call io_init()
111
113
117 call calc_mode_par%set_parallelization(p_strategy_states, default = .false.)
118 sys => electrons_t(global_namespace, generate_epot=.false.)
119 call sys%init_parallelization(mpi_world)
120
121 !%Variable Wannier90Prefix
122 !%Type string
123 !%Default w90
124 !%Section Utilities::oct-wannier90
125 !%Description
126 !% Prefix for wannier90 files
127 !%End
128 call parse_variable(global_namespace, 'Wannier90Prefix', 'w90', w90_prefix)
129 if (w90_prefix == 'w90') then
130 message(1) = "oct-wannier90: the prefix is set by default to w90"
131 call messages_info(1)
132 end if
133
134 !%Variable Wannier90Mode
135 !%Type integer
136 !%Default 0
137 !%Section Utilities::oct-wannier90
138 !%Description
139 !% Specifies which stage of the Wannier90 interface to use
140 !%Option none 0
141 !% Nothing is done.
142 !%Option w90_setup 1
143 !% Writes parts of the wannier90 input file <tt>w90_prefix.win</tt> corresponding to
144 !% the octopus inp file. Importantly it generates the correct form of Monkhorst-Pack mesh
145 !% written to the file w90_kpoints that has to be used in a gs calculation of Octopus by
146 !% as <tt> include w90_kpoints </tt> instead of the <tt>%KpointsGrid</tt> block.
147 !%Option w90_output 2
148 !% Generates the relevant files for a wannier90 run, specified by the variable <tt>W90_interface_files</tt>.
149 !% This needs files previously generated
150 !% by <tt>wannier90.x -pp w90 </tt>
151 !%Option w90_wannier 3
152 !% Parse the output of wannier90 to generate the Wannier states on the real-space grid.
153 !% The states will be written in the folder wannier. By default, the states are written as
154 !% binary files, similar to the Kohn-Sham states.
155 !%
156 !% Not implemented for spinor states.
157 !%End
158 call parse_variable(global_namespace, 'Wannier90Mode', 0, w90_mode)
159
160 if (w90_mode == 0) then
161 message(1) = "Wannier90Mode must be set to a value different from 0."
162 call messages_fatal(1)
163 end if
164
165 !%Variable Wannier90Files
166 !%Type flag
167 !%Default w90_mmn + w90_amn + w90_eig
168 !%Section Utilities::oct-wannier90
169 !%Description
170 !% Specifies which files to generate.
171 !% Example: <tt>w90_mmn + w90_unk</tt>
172 !%Option w90_mmn bit(1)
173 !% (see Wannier90 documentation)
174 !%Option w90_unk bit(2)
175 !% (see Wannier90 documentation)
176 !%Option w90_amn bit(3)
177 !% (see Wannier90 documentation)
178 !%Option w90_eig bit(4)
179 !% Eigenvalues. See Wannier90 documentation for more details.
180 !%End
181 w90_what_default = option__wannier90files__w90_mmn + option__wannier90files__w90_amn + option__wannier90files__w90_eig
182 call parse_variable(global_namespace, 'Wannier90Files', w90_what_default, w90_what)
183
184 !%Variable Wannier90UseTD
185 !%Type logical
186 !%Default no
187 !%Section Utilities::oct-wannier90
188 !%Description
189 !% By default oct-wannier90 uses the ground-state states to compute the necessary information.
190 !% By setting this variable to yes, oct-wannier90 will use the TD states instead.
191 !%End
192 call parse_variable(global_namespace, 'Wannier90UseTD', .false., read_td_states)
193
194 !%Variable Wannier90UseSCDM
195 !%Type logical
196 !%Default no
197 !%Section Utilities::oct-wannier90
198 !%Description
199 !% By default oct-wannier90 uses the projection method to generate the .amn file.
200 !% By setting this variable to yes, oct-wannier90 will use SCDM method instead.
201 !%End
202 call parse_variable(global_namespace, 'Wannier90UseSCDM', .false., w90_scdm)
203 if (w90_scdm) then
204 !%Variable SCDMsigma
205 !%Type float
206 !%Default 0.2
207 !%Section Utilities::oct-wannier90
208 !%Description
209 !% Broadening of SCDM smearing function.
210 !%End
211 call parse_variable(global_namespace, 'SCDMsigma', 0.2_real64, scdm_sigma)
212
213 !%Variable SCDMmu
214 !%Type float
215 !%Section Utilities::oct-wannier90
216 !%Description
217 !% Energy range up to which states are considered for SCDM.
218 !%End
219 call parse_variable(global_namespace, 'SCDMmu', m_huge, scdm_mu)
220 end if
221
222 if (sys%kpoints%use_symmetries) then
223 message(1) = 'oct-wannier90: k-points symmetries are not allowed'
224 call messages_fatal(1)
225 end if
226 if (sys%kpoints%use_time_reversal) then
227 message(1) = 'oct-wannier90: time-reversal symmetry is not allowed'
228 call messages_fatal(1)
229 end if
230 if (sys%kpoints%reduced%nshifts > 1) then
231 message(1) = 'oct-wannier90: Wannier90 does not allow for multiple shifts of the k-point grid'
232 call messages_fatal(1)
233 end if
234
235
236 if (sys%st%d%ispin /= unpolarized) then
237 call messages_experimental("oct-wannier90 with SpinComponnents /= unpolarized")
238 end if
239
240 w90_spinors = .false.
241 w90_spin_channel = 1
242
243 ! "Correct" rlattice/klattice for Wannier90 (3D periodicity assumed here).
244 factor = m_one
245 do idir = sys%space%periodic_dim+1, sys%space%dim
246 factor(idir) = m_two * sys%gr%box%bounding_box_l(idir)
247 end do
248 call sys%ions%latt%scale(factor)
249
250 ! create setup files
251 select case (w90_mode)
252 case (option__wannier90mode__w90_setup)
253 call wannier90_setup(sys%ions, sys%kpoints, sys%space)
254
255 ! load states and calculate interface files
256 case (option__wannier90mode__w90_output)
257 call wannier90_output()
258
259 case (option__wannier90mode__w90_wannier)
261
262 ! normal interface run
263 call states_elec_allocate_wfns(sys%st, sys%gr, wfs_type = type_cmplx, skip=exclude_list)
264 if (read_td_states) then
265 call restart%init(global_namespace, restart_td, restart_type_load, &
266 sys%mc, ierr, sys%gr)
267 else
268 call restart%init(global_namespace, restart_gs, restart_type_load, &
269 sys%mc, ierr, sys%gr)
270 end if
271
272 if (ierr == 0) then
273 call states_elec_look(restart, nik, dim, nst, ierr)
274 if (dim == sys%st%d%dim .and. nik == sys%kpoints%reduced%npoints .and. nst == sys%st%nst) then
275 call states_elec_load(restart, global_namespace, sys%space, sys%st, sys%gr, sys%kpoints, &
276 ierr, iter, label = ": wannier90", skip=exclude_list)
277 else
278 write(message(1),'(a)') 'Restart structure not commensurate.'
279 call messages_fatal(1)
280 end if
281 end if
282 call restart%end()
283
284 call generate_wannier_states(sys%space, sys%gr, sys%ions, sys%st, sys%kpoints)
285 case default
286 message(1) = "Wannier90Mode is set to an unsupported value."
287 call messages_fatal(1)
288 end select
289
290 safe_deallocate_a(exclude_list)
291 safe_deallocate_a(band_index)
292 safe_deallocate_a(w90_nnk_list)
293 safe_deallocate_a(w90_proj_centers)
294 safe_deallocate_a(w90_proj_lmr)
295
296 safe_deallocate_p(sys)
297 call fft_all_end()
298 call io_end()
300 call messages_end()
301 call parser_end()
302 call global_end()
303
304contains
305
306 ! --------------------------------------------------------------------------
307 subroutine wannier90_setup(ions, kpoints, space)
308 type(ions_t), intent(in) :: ions
309 type(kpoints_t), intent(in) :: kpoints
310 class(space_t), intent(in) :: space
311
312 character(len=80) :: filename
313 integer :: w90_win, ia, axis(3), npath
314
315 push_sub(wannier90_setup)
316
317 assert(space%dim == 3)
318
319 ! open win file
320 filename = trim(adjustl(w90_prefix)) //'.win'
321 w90_win = io_open(trim(filename), global_namespace, action='write')
322
323 write(w90_win,'(a)') '# this file has been created by the Octopus wannier90 utility'
324 write(w90_win,'(a)') ' '
325
326 ! write direct lattice vectors (in angstrom)
327 write(w90_win,'(a)') 'begin unit_cell_cart'
328 write(w90_win,'(a)') 'Ang'
329 do idim = 1,3
330 write(w90_win,'(f13.8,f13.8,f13.8)') units_from_atomic(unit_angstrom, ions%latt%rlattice(1:3,idim))
331 end do
332 write(w90_win,'(a)') 'end unit_cell_cart'
333 write(w90_win,'(a)') ' '
334
335 write(w90_win,'(a)') 'begin atoms_frac'
336 do ia = 1, ions%natoms
337 write(w90_win,'(a,2x,f13.8,f13.8,f13.8)') trim(ions%atom(ia)%label), ions%latt%cart_to_red(ions%pos(:, ia))
338 end do
339 write(w90_win,'(a)') 'end atoms_frac'
340 write(w90_win,'(a)') ' '
341
342 ! This is a default value. In practice, one should use projections
343 write(w90_win,'(a)') 'use_bloch_phases = .true.'
344 write(w90_win,'(a)') ' '
345
346 write(w90_win,'(a10,i4)') 'num_bands ', sys%st%nst
347 write(w90_win,'(a9,i4)') 'num_wann ', sys%st%nst
348 write(w90_win,'(a)') ' '
349
350 if (sys%st%d%ispin == spinors) then
351 write(w90_win,'(a)') 'spinors = .true.'
352 end if
353 if (sys%st%d%ispin == spin_polarized) then
354 write(w90_win, '(a)') 'spin = up'
355 end if
356
357 ! This is for convenience. This is needed for plotting the Wannier states, if requested.
358 write(w90_win,'(a)') 'write_u_matrices = .true.'
359 write(w90_win,'(a)') 'write_xyz = .true.'
360 write(w90_win,'(a)') ' '
361
362 if (kpoints%reduced%npoints == 1) then
363 write(w90_win,'(a)') 'gamma_only = .true.'
364 write(w90_win,'(a)') ' '
365 else
366 if (.not. parse_is_defined(global_namespace, 'KPointsGrid')) then
367 message(1) = 'oct-wannier90: need Monkhorst-Pack grid. Please specify %KPointsGrid'
368 call messages_fatal(1)
369 end if
370
371 ! In case the user used also a k-point path, we ignore it
372 npath = kpoints%nkpt_in_path()
373
374 axis(1:3) = kpoints%nik_axis(1:3)
375 assert(product(kpoints%nik_axis(1:3)) == kpoints%reduced%npoints - npath)
376
377 write(w90_win,'(a8,i4,i4,i4)') 'mp_grid =', axis(1:3)
378 write(w90_win,'(a)') ' '
379 write(w90_win,'(a)') 'begin kpoints '
380 ! Put a minus sign here for the wrong convention in Octopus
381
382 do ii = 1, kpoints%reduced%npoints-npath
383 write(w90_win,'(f13.8,f13.8,f13.8)') - kpoints%reduced%red_point(1:3,ii)
384 end do
385 write(w90_win,'(a)') 'end kpoints '
386 end if
387
388 call io_close(w90_win)
389
390 pop_sub(wannier90_setup)
391
392 end subroutine wannier90_setup
393
394 ! --------------------------------------------------------------------------
395 subroutine wannier90_output()
396 integer :: ik_real
397
398 push_sub(wannier90_output)
399
401
402 ! normal interface run
403 call states_elec_allocate_wfns(sys%st, sys%gr, wfs_type = type_cmplx, skip=exclude_list)
404 if (read_td_states) then
405 call restart%init(global_namespace, restart_td, restart_type_load, &
406 sys%mc, ierr, sys%gr)
407 else
408 call restart%init(global_namespace, restart_gs, restart_type_load, &
409 sys%mc, ierr, sys%gr)
410 end if
411
412 if (ierr == 0) then
413 call states_elec_look(restart, nik, dim, nst, ierr)
414 if (sys%st%d%ispin == spin_polarized) then
415 nik = nik / 2
416 end if
417 if (dim == sys%st%d%dim .and. nik == sys%kpoints%reduced%npoints .and. nst == sys%st%nst) then
418 call states_elec_load(restart, global_namespace, sys%space, sys%st, sys%gr, sys%kpoints, &
419 ierr, iter, label = ": wannier90", skip=exclude_list)
420 else
421 write(message(1),'(a)') 'Restart structure not commensurate.'
422 call messages_fatal(1)
423 end if
424 end if
425 call restart%end()
426
427 if (w90_scdm) then
428 nik = w90_num_kpts
429 safe_allocate(jpvt(1:sys%gr%np_global*sys%st%d%dim))
430 safe_allocate(psi(1:sys%gr%np, 1:sys%st%d%dim))
431 safe_allocate(occ_temp(1:w90_num_bands))
432
433 ! smear the states at gamma
434 do ist = 1, w90_num_bands
435 occ_temp(ist)= sys%st%occ(ist, 1)
436 sys%st%occ(ist, 1)=m_half*loct_erfc((sys%st%eigenval(ist, 1)-scdm_mu) / scdm_sigma)
437 end do
438
439 call zstates_elec_rrqr_decomposition(sys%st, sys%namespace, sys%gr, w90_num_bands, .true., 1, jpvt)
440
441 ! reset occupations at gamma
442 do ist = 1, w90_num_bands
443 sys%st%occ(ist, 1) = occ_temp(ist)
444 end do
445
446 safe_allocate(uk(1:w90_num_bands, 1:w90_num_bands, 1:nik))
447
448 ! auxiliary arrays for scdm procedure
449 safe_allocate(chi(1:w90_num_bands, 1:w90_num_bands))
450 safe_allocate(chi_diag(1:w90_num_bands, 1:w90_num_bands))
451 safe_allocate(chi2(1:w90_num_bands, 1:w90_num_bands))
452 safe_allocate(chi_eigenval(1:w90_num_bands))
453
454 chi(1:w90_num_bands, 1:w90_num_bands) = m_zero
455
456 do ik = 1, nik
457 kvec(:) = sys%kpoints%reduced%point(:, ik)
458
459 if (sys%st%d%ispin == spin_polarized) then
460 ik_real = (ik-1)*2 + w90_spin_channel
461 else
462 ik_real = ik
463 end if
464
465 do ist = 1, w90_num_bands
466 call states_elec_get_state(sys%st, sys%gr, ist, ik_real, psi)
467 smear=m_half * loct_erfc((sys%st%eigenval(ist, ik_real) - scdm_mu) / scdm_sigma)
468 ! NOTE: here check for domain parallelization
469 do jst = 1, w90_num_bands
470 chi(ist, jst) = smear * conjg(psi(jpvt(jst), 1)) &
471 * exp(m_zi * dot_product(sys%gr%x(jpvt(jst), 1:3), kvec(1:3)))
472 end do
473 end do
474
475 ! loewdin orhtogonalization of chi.chi
476 ! this can also be done with SVD, which might be more stable!?
477 chi_diag = matmul(conjg(transpose(chi)), chi)
478 call lalg_eigensolve(w90_num_bands, chi_diag, chi_eigenval)
479 chi2 = conjg(transpose(chi_diag))
480
481 !we need the eigenvalues to be >0
482 if (any(chi_eigenval(:) .lt. m_zero)) then
483 message(1) = 'SCDM Wannierization failed because chi matrix is'
484 message(2) = 'ill conditioned. Try increasingin scdm_sigma and/or'
485 message(3) = 'change scdm_mu.'
486 call messages_fatal(3)
487 end if
488
489 do ist = 1, w90_num_bands
490 chi_eigenval(ist) = m_one / sqrt(chi_eigenval(ist))
491 chi2(ist, 1:w90_num_bands) = chi_eigenval(ist) * chi2(ist, 1:w90_num_bands)
492 end do
493 ! the loewdin result would be: matmul(chi_diag,chi2)
494 ! to get the wannier gauge U(k) we multiply this with the original chi
495 uk(:,:,ik) = matmul(chi, matmul(chi_diag,chi2))
496
497 end do
498
499 safe_deallocate_a(chi)
500 safe_deallocate_a(psi)
501 safe_deallocate_a(chi_diag)
502 safe_deallocate_a(chi2)
503 safe_deallocate_a(chi_eigenval)
504 safe_deallocate_a(jpvt)
505 safe_deallocate_a(psi)
506 safe_deallocate_a(occ_temp)
507 end if
508
509 ! ---- actual interface work ----------
510 if (bitand(w90_what, option__wannier90files__w90_mmn) /= 0) then
511 call create_wannier90_mmn(sys%gr, sys%st)
512 end if
513
514 if (bitand(w90_what, option__wannier90files__w90_unk) /= 0) then
515 call write_unk(sys%space, sys%gr, sys%st)
516 end if
517
518 if (bitand(w90_what, option__wannier90files__w90_amn) /= 0) then
519 call create_wannier90_amn(sys%space, sys%gr, sys%ions%latt, sys%st, sys%kpoints)
520 end if
521
522 if (bitand(w90_what, option__wannier90files__w90_eig) /= 0) then
524 end if
525
526 safe_deallocate_a(uk)
527 safe_deallocate_a(w90_spin_proj_component)
528 safe_deallocate_a(w90_spin_proj_axis)
529
530 pop_sub(wannier90_output)
531 end subroutine wannier90_output
532
533 ! --------------------------------------------------------------------------
534 subroutine read_wannier90_files()
535 integer :: w90_nnkp, itemp, dummyint, io
536 character(len=80) :: filename, dummy, dummy1, dummy2, line
537 logical :: exist, parse_is_ok
538 real(real64) :: dummyr(7)
539
540 push_sub(read_wannier90_files)
541
542 w90_num_kpts = product(sys%kpoints%nik_axis(1:3))
543 w90_num_exclude = 0
544
545 ! open nnkp file
546 filename = trim(adjustl(w90_prefix)) //'.nnkp'
547
548 message(1) = "oct-wannier90: Parsing "//filename
549 call messages_info(1)
550
551 inquire(file=filename,exist=exist)
552 if (.not. exist) then
553 message(1) = 'oct-wannier90: Cannot find specified Wannier90 nnkp file.'
554 write(message(2),'(a)') 'Please run wannier90.x -pp '// trim(adjustl(w90_prefix)) // ' first.'
555 call messages_fatal(2)
556 end if
557
558 parse_is_ok = .false.
559
560 ! check number of k-points
561 w90_nnkp = io_open(trim(filename), global_namespace, action='read')
562 do
563 read(w90_nnkp, *, iostat=io) dummy, dummy1
564 if (io == iostat_end) exit
565
566 if (dummy == 'begin' .and. dummy1 == 'kpoints') then
567 read(w90_nnkp,*) itemp
568 if (itemp /= w90_num_kpts) then
569 message(1) = 'oct-wannier90: wannier90 setup seems to have been done with a different number of k-points.'
570 call messages_fatal(1)
571 else
572 parse_is_ok = .true.
573 exit
574 end if
575 end if
576 end do
577 call io_close(w90_nnkp)
578
579 if (.not. parse_is_ok) then
580 message(1) = 'oct-wannier90: Did not find the kpoints block in nnkp file'
581 call messages_fatal(1)
582 end if
583 parse_is_ok = .false.
584
585 ! read from nnkp file
586 ! find the nnkpts block
587 w90_nnkp = io_open(trim(filename), global_namespace, action='read', position='rewind')
588 do
589 read(w90_nnkp, *, iostat=io) dummy, dummy1
590 if (io == iostat_end) exit !End of file
591
592 if (dummy == 'begin' .and. dummy1 == 'nnkpts') then
593 read(w90_nnkp,*) w90_nntot
594 safe_allocate(w90_nnk_list(1:5, 1:w90_num_kpts * w90_nntot))
595 do ii = 1, w90_num_kpts * w90_nntot
596 read(w90_nnkp,*) w90_nnk_list(1:5, ii)
597 end do
598 !make sure we are at the end of the block
599 read(w90_nnkp,*) dummy
600 if (dummy /= 'end') then
601 message(1) = 'oct-wannier90: There dont seem to be enough k-points in nnkpts file to.'
602 call messages_fatal(1)
603 end if
604 parse_is_ok = .true.
605 exit
606 end if
607 end do
608
609 if (.not. parse_is_ok) then
610 message(1) = 'oct-wannier90: Did not find nnkpts block in nnkp file'
611 call messages_fatal(1)
612 end if
613
614 ! read from nnkp file
615 ! find the exclude block
616 safe_allocate(exclude_list(1:sys%st%nst))
617 !By default we use all the bands
618 exclude_list(1:sys%st%nst) = .false.
619 rewind(w90_nnkp)
620 do
621 read(w90_nnkp, *, iostat=io) dummy, dummy1
622 if (io == iostat_end) exit !End of file
623 if (dummy == 'begin' .and. dummy1 == 'exclude_bands') then
624 read(w90_nnkp, *) w90_num_exclude
625 do ii = 1, w90_num_exclude
626 read(w90_nnkp, *) itemp
627 if(itemp > sys%st%nst) then
628 message(1) = 'oct-wannier90: The exclude_bands list contains a state index higher than the number of states.'
630 end if
631 exclude_list(itemp) = .true.
632 end do
633 !make sure we are at the end of the block
634 read(w90_nnkp, *) dummy
635 if (dummy /= 'end') then
636 message(1) = 'oct-wannier90: There dont seem to be enough bands in exclude_bands list.'
637 call messages_fatal(1)
638 end if
639 exit
640 end if
641 end do
642 call io_close(w90_nnkp)
643
644 !We get the number of bands
645 w90_num_bands = sys%st%nst - w90_num_exclude
646
647 safe_allocate(band_index(1:sys%st%nst))
648 itemp = 0
649 do ii = 1, sys%st%nst
650 if (exclude_list(ii)) cycle
651 itemp = itemp + 1
652 band_index(ii) = itemp
653 end do
654
655 if (bitand(w90_what, option__wannier90files__w90_amn) /= 0 &
656 .or. w90_mode == option__wannier90mode__w90_wannier ) then
657 ! parse file again for definitions of projections
658 w90_nnkp = io_open(trim(filename), global_namespace, action='read', position='rewind')
659
660 do
661 read(w90_nnkp, *, iostat=io) dummy, dummy1
662 if (io == iostat_end) then !End of file
663 message(1) = 'oct-wannier90: Did not find projections block in w90.nnkp file'
664 call messages_fatal(1)
665 end if
666
667 if (dummy == 'begin' .and. (dummy1 == 'projections' .or. dummy1 == 'spinor_projections')) then
668
669 if (dummy1 == 'spinor_projections') then
670 w90_spinors = .true.
671 if (sys%st%d%ispin /= spinors) then
672 message(1) = 'oct-wannier90: Spinor = .true. is only valid with spinors wavefunctions.'
673 call messages_fatal(1)
674 end if
675
676 message(1) = 'oct-wannier90: Spinor interface incomplete. Note there is no quantization axis implemented'
677 call messages_warning(1)
678 else
679 if (sys%st%d%ispin == spinors) then
680 message(1) = 'oct-wannier90: Octopus has spinors wavefunctions but spinor_projections is not defined.'
681 message(2) = 'oct-wannier90: Please check the input file for wannier 90.'
682 call messages_fatal(2)
683 end if
684 end if
685
686 read(w90_nnkp, *) w90_nproj
687 ! num_wann is given in w90.win, not double checked here
688 ! I assume that the wannier90.x -pp run has checked this
689 w90_num_wann = w90_nproj
690 ! In case of no projections, we use the number of bands
691 if(w90_nproj == 0) w90_num_wann = w90_num_bands
692
693 safe_allocate(w90_proj_centers(1:3, 1:w90_nproj))
694 safe_allocate(w90_proj_lmr(1:w90_nproj, 1:3))
695 if (w90_spinors) then
696 safe_allocate(w90_spin_proj_component(1:w90_nproj))
697 end if
698 if (w90_spinors) then
699 safe_allocate(w90_spin_proj_axis(1:w90_nproj, 1:3))
700 end if
701
702 do ii = 1, w90_nproj
703 read(w90_nnkp, *) w90_proj_centers(1:3, ii), w90_proj_lmr(ii, 1:3)
704 ! skip a line for now
705 read(w90_nnkp, *) dummyr(1:7)
706 if (w90_spinors) then
707 read(w90_nnkp, *) w90_spin_proj_component(ii), w90_spin_proj_axis(ii, 1:3)
708 ! use octopus spindim conventions
709 if (w90_spin_proj_component(ii) == -1) w90_spin_proj_component(ii) = 2
710 end if
711 end do
712 !make sure we are at the end of the block
713 read(w90_nnkp, *) dummy
714 if (dummy /= 'end') then
715 message(1) = 'oct-wannier90: There dont seem to be enough projections in nnkpts file to.'
716 call messages_fatal(1)
717 end if
718 exit
719 end if
720 end do
721
722 ! look for auto_projection block
723 scdm_proj = .false.
724 do
725 read(w90_nnkp, *, iostat=io) dummy, dummy1
726 if (io == iostat_end) exit !End of file
727
728 if (dummy == 'begin' .and. dummy1 == 'auto_projections') then
729 scdm_proj = .true.
730 read(w90_nnkp, *) w90_nproj
731 w90_num_wann = w90_nproj
732
733 if (.not. w90_scdm) then
734 message(1) = 'oct-wannier90: Found auto_projections block. Currently the only implemented automatic way'
735 message(2) = 'oct-wannier90: to compute projections is the SCDM method.'
736 message(3) = 'oct-wannier90: Please set Wannier90UseSCDM = yes in the inp file.'
737 call messages_fatal(3)
738 end if
739
740 if (w90_nproj /= w90_num_bands) then
741 message(1) = 'oct-wannier90: In auto_projections block first row needs to be equal to num_bands.'
742 call messages_fatal(1)
743 end if
744 read(w90_nnkp, *) dummyint
745 if (dummyint /= 0) then
746 message(1) = 'oct-wannier90: The second row in auto_projections has to be 0, per Wannier90 documentation.'
747 call messages_fatal(1)
748 end if
749 end if
750 end do
751 call io_close(w90_nnkp)
752
753 end if
754
755 message(1) = "oct-wannier90: Finished parsing "//filename
756 call messages_info(1)
757
758 ! Look extra variables variable
759 ! open win file
760 filename = trim(adjustl(w90_prefix)) //'.win'
761 message(1) = "oct-wannier90: Parsing "//filename
762 call messages_info(1)
763 w90_nnkp = io_open(trim(filename), global_namespace, action='read', position='rewind')
764 do
765 read(w90_nnkp, fmt='(a)', iostat=io) line
766 if (io == iostat_end) exit !End of file
767 if (index(line, '=') > 0) then
768 read(line, *, iostat=io) dummy, dummy2, dummy1
769 else
770 read(line, *, iostat=io) dummy, dummy1
771 end if
772
773 !Spin
774 if (dummy == 'spin') then
775 if (sys%st%d%ispin /= spin_polarized) then
776 message(1) = 'oct-wannier90: The variable spin is set for a non spin-polarized calculation.'
777 call messages_fatal(1)
778 end if
779
780 if (dummy1 == 'up') then
781 w90_spin_channel = 1
782 else if (dummy1 == 'down') then
783 w90_spin_channel = 2
784 else
785 message(1) = 'oct-wannier90: Error parsing the variable spin.'
786 call messages_fatal(1)
787 end if
788 end if
789 end do
790 call io_close(w90_nnkp)
791
792 if (sys%st%d%ispin == spin_polarized) then
793 write(message(1), '(a,i1)') 'oct-wannier90: Using spin channel ', w90_spin_channel
794 call messages_info(1)
795 end if
796
797 message(1) = "oct-wannier90: Finished parsing "//filename
798 call messages_info(1)
799
800 pop_sub(read_wannier90_files)
801
802 end subroutine read_wannier90_files
803
804 ! --------------------------------------------------------------------------
805 subroutine create_wannier90_mmn(mesh, st)
806 class(mesh_t), intent(in) :: mesh
807 type(states_elec_t), target, intent(in) :: st
808
809 integer :: ist, jst, ik, ip, w90_mmn, iknn, idim, ibind
810 real(real64) :: Gcart(3)
811 integer :: G(3)
812 character(len=80) :: filename
813 complex(real64), allocatable :: overlap(:)
814 complex(real64), allocatable :: psim(:,:), psin(:,:), phase(:)
815 type(wfs_elec_t), pointer :: batch
816 integer :: inode, node_fr, node_to
817 type(mpi_request) :: send_req
818
819 push_sub(create_wannier90_mmn)
820
821 call profiling_in("W90_MMN")
822
823 if (st%parallel_in_states) then
824 call messages_not_implemented("w90_mmn output with states parallelization")
825 end if
826
827 message(1) = "Info: Computing the overlap matrix"
828 call messages_info(1)
829
830
831 filename = './'// trim(adjustl(w90_prefix))//'.mmn'
832 w90_mmn = io_open(trim(filename), global_namespace, action='write')
833
834 ! write header
835 if (mpi_world%is_root()) then
836 write(w90_mmn,*) 'Created by oct-wannier90'
837 write(w90_mmn,*) w90_num_bands, w90_num_kpts, w90_nntot
838 end if
839
840 safe_allocate(psim(1:mesh%np, 1:st%d%dim))
841 safe_allocate(psin(1:mesh%np, 1:st%d%dim))
842 safe_allocate(phase(1:mesh%np))
843 safe_allocate(overlap(1:w90_num_bands))
844
845
846 ! loop over the pairs specified in the nnkp file (read before in init)
847 do ii = 1, w90_num_kpts * w90_nntot
848 ik = w90_nnk_list(1, ii)
849 iknn = w90_nnk_list(2, ii)
850 g(1:3) = w90_nnk_list(3:5, ii)
851 if (mpi_world%is_root()) write(w90_mmn, '(I10,2x,I10,2x,I3,2x,I3,2x,I3)') ik, iknn, g
852
853 ! For spin-polarized calculations, we select the right k-point
854 if (sys%st%d%ispin == spin_polarized) then
855 ik = (ik-1)*2 + w90_spin_channel
856 iknn = (iknn-1)*2 + w90_spin_channel
857 end if
858
859
860 ! Only treat local k-points
861 if(ik >= st%d%kpt%start .and. ik <= st%d%kpt%end) then
862
863 ! Wannier90 treats everything fully periodic
864 ! Conversion is done with the 3D "correct" klattice
865 call kpoints_to_absolute(sys%ions%latt, real(g, real64) , gcart)
866
867 ! Phase that gives u_{n,k+G}(r) from u_{nk}(r)
868 ! Here the minus sign of Octopus cancels with minus sign of the input G
869 ! (ik and iknn correspond in the list to minus the k-points in Octopus)
870 if (any(g /= 0)) then
871 do ip = 1, mesh%np
872 phase(ip) = exp(-m_zi*dot_product(mesh%x(ip,1:3), gcart(1:3)))
873 end do
874 end if
875
876 end if
877
878 ! Loop over distributed bands
879 do jst = 1, st%nst
880 if (exclude_list(jst)) cycle
881
882 ! Communication for the local states
883 if ( .not. st%d%kpt%parallel .and. .not. st%parallel_in_states) then
884 call states_elec_get_state(st, mesh, jst, iknn, psin)
885 else
886 node_fr = -1
887 node_to = -1
888 do inode = 0, st%d%kpt%mpi_grp%size-1
889 if(iknn >= st%st_kpt_task(inode,3) .and. iknn <= st%st_kpt_task(inode,4)) then
890 node_fr = inode
891 end if
892 if(ik >= st%st_kpt_task(inode,3) .and. ik <= st%st_kpt_task(inode,4)) then
893 node_to = inode
894 end if
895 end do
896 assert(node_fr > -1)
897 assert(node_to > -1)
898
899 send_req = mpi_request_null
900 ! We have locally the k-point
901 if (state_kpt_is_local(st, jst, iknn)) then
902 call states_elec_get_state(st, mesh, jst, iknn, psin)
903 ! We send it only if we don`t want to use it locally
904 if(node_to /= st%d%kpt%mpi_grp%rank) then
905 call st%d%kpt%mpi_grp%isend(psin, mesh%np*st%d%dim, mpi_double_complex, node_to, send_req)
906 end if
907 end if
908 ! We receive the desired state, only if it is not a local one
909 if(node_to == st%d%kpt%mpi_grp%rank .and. node_to /= node_fr) then
910 call st%d%kpt%mpi_grp%recv(psin, mesh%np*st%d%dim, mpi_double_complex, node_fr)
911 end if
912 if (send_req /= mpi_request_null) then
913 call st%d%kpt%mpi_grp%wait(send_req)
914 end if
915 end if
916
917 overlap = m_zero
918
919 if(ik >= st%d%kpt%start .and. ik <= st%d%kpt%end) then
920
921 ! Do not apply the phase if the phase factor is null
922 if (any(g /= 0)) then
923 ! add phase
924 do idim = 1, st%d%dim
925 do ip = 1, mesh%np
926 psin(ip, idim) = psin(ip, idim) * phase(ip)
927 end do
928 end do
929 end if
930
931
932 ! See Eq. (25) in PRB 56, 12847 (1997)
933 ! Loop over local k-points
934 do ist = 1, st%nst
935 if (exclude_list(ist)) cycle
936
937 batch => st%group%psib(st%group%iblock(ist), ik)
938
939 select case (batch%status())
940 case (batch_not_packed)
941 overlap(band_index(ist)) = m_z0
942 do idim = 1, st%d%dim
943 ibind = batch%inv_index((/ist, idim/))
944 overlap(band_index(ist)) = overlap(band_index(ist)) + &
945 zmf_dotp(mesh, batch%zff_linear(:, ibind), psin(:,idim), reduce = .false.)
946 end do
947 !Not properly done at the moment
949 call states_elec_get_state(st, mesh, ist, ik, psim)
950 overlap(band_index(ist)) = zmf_dotp(mesh, st%d%dim, psim, psin, reduce = .false.)
951 end select
952 end do !ist
953 end if
954
955 call profiling_in("W90_MMN_REDUCE")
956 call mesh%allreduce(overlap)
957 call profiling_out("W90_MMN_REDUCE")
958
959 if(st%d%kpt%parallel) then
960 call comm_allreduce(st%d%kpt%mpi_grp, overlap)
961 end if
962
963 ! write to W90 file
964 if (mpi_world%is_root()) then
965 do ist = 1, st%nst
966 if (exclude_list(ist)) cycle
967 write(w90_mmn,'(e18.10,2x,e18.10)') overlap(band_index(ist))
968 end do
969 end if
970
971 end do !jst
972 end do
973
974 call io_close(w90_mmn)
975
976 safe_deallocate_a(psim)
977 safe_deallocate_a(psin)
978 safe_deallocate_a(phase)
979 safe_deallocate_a(overlap)
980
981 call profiling_out("W90_MMN")
982
983 pop_sub(create_wannier90_mmn)
984
985 end subroutine create_wannier90_mmn
986
987 ! --------------------------------------------------------------------------
988 subroutine create_wannier90_eig()
989 integer :: ist, ik, w90_eig
990 character(len=80) :: filename
991
992 push_sub(create_wannier90_eig)
993
994 if (sys%st%parallel_in_states) then
995 call messages_not_implemented("w90_eig output with states parallelization")
996 end if
997
998 if (mpi_world%is_root()) then
999 filename = './'//trim(adjustl(w90_prefix))//'.eig'
1000 w90_eig = io_open(trim(filename), global_namespace, action='write')
1001 do ik = 1, w90_num_kpts
1002 do ist = 1, sys%st%nst
1003 if (exclude_list(ist)) cycle
1004 if (sys%st%d%ispin /= spin_polarized) then
1005 write(w90_eig,'(I5,2x,I8,2x,e18.10)') band_index(ist), ik, &
1006 units_from_atomic(unit_ev, sys%st%eigenval(ist, ik))
1007 else
1008 write(w90_eig,'(I5,2x,I8,2x,e18.10)') band_index(ist), ik, &
1009 units_from_atomic(unit_ev, sys%st%eigenval(ist, (ik-1)*2+w90_spin_channel))
1010 end if
1011 end do
1012 end do
1013
1014 call io_close(w90_eig)
1015 end if
1016
1017 pop_sub(create_wannier90_eig)
1018 end subroutine create_wannier90_eig
1019
1020 ! --------------------------------------------------------------------------
1021 subroutine write_unk(space, mesh, st)
1022 class(space_t), intent(in) :: space
1023 class(mesh_t), intent(in) :: mesh
1024 type(states_elec_t), intent(in) :: st
1025
1026 integer :: ist, ik, unk_file, ispin
1027 integer :: ix, iy, iz
1028 character(len=80) :: filename
1029 complex(real64), allocatable :: psi(:)
1030 type(cube_t) :: cube
1031 type(cube_function_t) :: cf
1032
1033 push_sub(write_unk)
1034
1035 if (st%d%kpt%parallel) then
1036 call messages_not_implemented("w90_unk output with k-point parallelization")
1037 end if
1038
1039 if (sys%gr%parallel_in_domains) then
1040 call messages_not_implemented("w90_unk output with domain parallelization")
1041 end if
1042
1043 if (st%parallel_in_states) then
1044 call messages_not_implemented("w90_unk output with states parallelization")
1045 end if
1046
1047 call messages_experimental("Wannier90Files = w90_unk")
1048
1049
1050 safe_allocate(psi(1:mesh%np))
1051
1052 call cube_init(cube, mesh%idx%ll, global_namespace, space, mesh%spacing, &
1053 mesh%coord_system, need_partition=.not.mesh%parallel_in_domains)
1054 call cube_init_cube_map(cube, mesh)
1055
1056 call zcube_function_alloc_rs(cube, cf)
1057
1058 do ik = 1, w90_num_kpts
1059 do ispin = 1, st%d%dim
1060 if (mpi_world%is_root()) then
1061 write(filename, '(a,i5.5,a1,i1)') './UNK', ik,'.', ispin
1062 unk_file = io_open(trim(filename), global_namespace, action='write', form='unformatted')
1063 ! write header
1064 write(unk_file) mesh%idx%ll(1:mesh%idx%dim), ik, w90_num_bands
1065 end if
1066
1067 ! states
1068 do ist = 1, st%nst
1069 if (exclude_list(ist)) cycle
1070
1071 if (sys%st%d%ispin /= spin_polarized) then
1072 call states_elec_get_state(st, mesh, ispin, ist, ik, psi)
1073 else
1074 call states_elec_get_state(st, mesh, ispin, ist, (ik-1)*2+w90_spin_channel, psi)
1075 end if
1076
1077 ! put the density in the cube
1078 ! Note: At the moment this does not work for domain parallelization
1079 if (cube%parallel_in_domains) then
1080 assert(.not. cube%parallel_in_domains)
1081 else
1082 call zmesh_to_cube(mesh, psi, cube, cf)
1083 end if
1084
1085 if (mpi_world%is_root()) then
1086 write(unk_file) (((cf%zrs(ix,iy,iz), ix=1,cube%rs_n_global(1)), iy=1,cube%rs_n_global(2)), iz=1,cube%rs_n_global(3))
1087 end if
1088 end do
1089 if (mpi_world%is_root()) call io_close(unk_file)
1090 end do
1091 end do
1092
1093 call dcube_function_free_rs(cube, cf)
1094 call cube_end(cube)
1095
1096 safe_deallocate_a(psi)
1097
1098 pop_sub(write_unk)
1099
1100 end subroutine write_unk
1101
1102 ! --------------------------------------------------------------------------
1103 subroutine create_wannier90_amn(space, mesh, latt, st, kpoints)
1104 class(space_t), intent(in) :: space
1105 class(mesh_t), intent(in) :: mesh
1106 type(lattice_vectors_t), intent(in) :: latt
1107 type(states_elec_t), intent(in) :: st
1108 type(kpoints_t), intent(in) :: kpoints
1109
1110 integer :: ist, ik, w90_amn, idim, iw, ip, ik_real
1111 real(real64) :: center(3), kpoint(3), threshold
1112 character(len=80) :: filename
1113 complex(real64), allocatable :: psi(:,:), phase(:), projection(:)
1114 real(real64), allocatable :: ylm(:)
1115 type(orbitalset_t), allocatable :: orbitals(:)
1117 push_sub(create_wannier90_amn)
1118 call profiling_in("W90_AMN")
1119
1120 if (st%parallel_in_states) then
1121 call messages_not_implemented("w90_amn output with states parallelization")
1122 end if
1123
1124 filename = './'// trim(adjustl(w90_prefix))//'.amn'
1125 w90_amn = io_open(trim(filename), global_namespace, action='write')
1126
1127 ! write header
1128 if (mpi_world%is_root()) then
1129 write(w90_amn,*) 'Created by oct-wannier90'
1130 write(w90_amn,*) w90_num_bands, w90_num_kpts, w90_num_wann
1131 end if
1132
1133 if (scdm_proj) then
1134
1135 message(1) = "Info: Writing projections obtained from SCDM."
1136 call messages_info(1)
1137
1138 if (st%d%kpt%parallel) then
1139 call messages_not_implemented("w90_amn output with k-point parallelization")
1140 end if
1141
1142
1143 do ik = 1, w90_num_kpts
1144 do ist = 1, st%nst
1145 if (exclude_list(ist)) cycle
1146 if (mpi_world%is_root()) then
1147 do iw = 1, w90_nproj
1148 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)
1149 end do
1150 end if
1151 end do !ist
1152 end do! ik
1153
1154 else
1155
1156 message(1) = "Info: Computing the projection matrix"
1157 call messages_info(1)
1158
1159 !We use the variable AOThreshold to deterine the threshold on the radii of the atomic orbitals
1160 call parse_variable(global_namespace, 'AOThreshold', 0.001_real64, threshold)
1161
1162 safe_allocate(orbitals(1:w90_nproj))
1163 ! precompute orbitals
1164 do iw=1, w90_nproj
1165 call orbitalset_init(orbitals(iw))
1166
1167 orbitals(iw)%norbs = 1
1168 orbitals(iw)%ndim = 1
1169 orbitals(iw)%radius = -log(threshold)
1170 orbitals(iw)%use_submesh = .false.
1171
1172 ! cartesian coordinate of orbital center
1173 center(1:3) = latt%red_to_cart(w90_proj_centers(1:3, iw))
1174 call submesh_init(orbitals(iw)%sphere, space, mesh, latt, center, orbitals(iw)%radius)
1175
1176 ! get dorb as submesh points
1177 safe_allocate(ylm(1:orbitals(iw)%sphere%np))
1178 ! (this is a routine from pwscf)
1179 call ylm_wannier(ylm, w90_proj_lmr(iw,1), w90_proj_lmr(iw,2), &
1180 orbitals(iw)%sphere%r, orbitals(iw)%sphere%rel_x, orbitals(iw)%sphere%np)
1181
1182 ! apply radial function
1183 if (w90_proj_lmr(iw,3) == 1) then
1184 do ip = 1,orbitals(iw)%sphere%np
1185 ylm(ip) = ylm(ip)*m_two*exp(-orbitals(iw)%sphere%r(ip))
1186 end do
1187 else
1188 call messages_not_implemented("oct-wannier90: r/=1 for the radial part")
1189 end if
1190
1191 safe_allocate(orbitals(iw)%zorb(1:orbitals(iw)%sphere%np, 1, 1))
1192 orbitals(iw)%zorb(1:orbitals(iw)%sphere%np, 1, 1) = ylm(1:orbitals(iw)%sphere%np)
1193 safe_deallocate_a(ylm)
1194
1195 safe_allocate(orbitals(iw)%phase(1:orbitals(iw)%sphere%np, st%d%kpt%start:st%d%kpt%end))
1196 orbitals(iw)%phase(:,:) = m_z0
1197 safe_allocate(orbitals(iw)%eorb_mesh(1:mesh%np, 1, 1, st%d%kpt%start:st%d%kpt%end))
1198 orbitals(iw)%eorb_mesh(:,:,:,:) = m_z0
1199
1200 call orbitalset_update_phase(orbitals(iw), space%dim, st%d%kpt, kpoints, st%d%ispin == spin_polarized, &
1201 kpt_max = w90_num_kpts)
1202
1203 end do
1204
1205 safe_allocate(psi(1:mesh%np, 1:st%d%dim))
1206 safe_allocate(phase(1:mesh%np))
1207 safe_allocate(projection(1:w90_nproj))
1208
1209 do ik = 1, w90_num_kpts
1210 kpoint(1:space%dim) = kpoints%get_point(ik)
1211 do ip = 1, mesh%np
1212 phase(ip) = exp(-m_zi* sum(mesh%x(ip, 1:space%dim) * kpoint(1:space%dim)))
1213 end do
1214
1215 !For spin-polarized calculations, we select the right k-point
1216 if (st%d%ispin == spin_polarized) then
1217 ik_real = (ik-1)*2 + w90_spin_channel
1218 else
1219 ik_real = ik
1220 end if
1221
1222
1223 do ist = 1, st%nst
1224 if (exclude_list(ist)) cycle
1225
1226 projection = m_zero
1227
1228 if(ik_real >= st%d%kpt%start .and. ik_real <= st%d%kpt%end) then
1229 call states_elec_get_state(st, mesh, ist, ik_real, psi)
1230
1231 do idim = 1, st%d%dim
1232 !The minus sign is here is for the wrong convention of Octopus
1233 do ip = 1, mesh%np
1234 psi(ip, idim) = psi(ip, idim)*phase(ip)
1235 end do
1236 end do
1237
1238 do iw = 1, w90_nproj
1239 idim = 1
1240 if (w90_spinors) idim = w90_spin_proj_component(iw)
1241
1242 !At the moment the orbitals do not depend on idim
1243 !The idim index for eorb_mesh would be for a spin-resolved orbital like j=1/2
1244 projection(iw) = zmf_dotp(mesh, psi(1:mesh%np,idim), &
1245 orbitals(iw)%eorb_mesh(1:mesh%np,1,1,ik_real), reduce = .false.)
1246 end do
1247
1248 call profiling_in("W90_AMN_REDUCE")
1249 call mesh%allreduce(projection)
1250 call profiling_out("W90_AMN_REDUCE")
1251 end if
1252
1253 if(st%d%kpt%parallel) then
1254 call comm_allreduce(st%d%kpt%mpi_grp, projection)
1255 end if
1256
1257 if (mpi_world%is_root()) then
1258 do iw = 1, w90_nproj
1259 write (w90_amn,'(I5,2x,I5,2x,I5,2x,e18.10,2x,e18.10)') band_index(ist), iw, ik, projection(iw)
1260 end do
1261 end if
1262 end do !ist
1263 end do !ik
1264
1265 safe_deallocate_a(psi)
1266 safe_deallocate_a(phase)
1267 safe_deallocate_a(projection)
1268
1269 do iw = 1, w90_nproj
1270 call orbitalset_end(orbitals(iw))
1271 end do
1272 safe_deallocate_a(orbitals)
1273 end if
1274
1275 call io_close(w90_amn)
1276
1277 call profiling_out("W90_AMN")
1278
1279 pop_sub(create_wannier90_amn)
1280
1281 end subroutine create_wannier90_amn
1282
1283 ! --------------------------------------------------------------------------
1284 subroutine generate_wannier_states(space, mesh, ions, st, kpoints)
1285 class(space_t), intent(in) :: space
1286 class(mesh_t), intent(in) :: mesh
1287 type(ions_t), intent(in) :: ions
1288 type(states_elec_t), intent(in) :: st
1289 type(kpoints_t), intent(in) :: kpoints
1290
1291 integer :: w90_u_mat, w90_xyz, nwann, nik
1292 integer :: ik, iw, iw2, ip, ipmax, rankmax
1293 real(real64), allocatable :: centers(:,:)
1294 complex(real64), allocatable :: Umnk(:,:,:)
1295 complex(real64), allocatable :: zwn(:), psi(:,:), phase(:)
1296 character(len=MAX_PATH_LEN) :: fname
1297 real(real64) :: kpoint(3), wmod, wmodmax, xx(space%dim)
1298 character(len=2) :: dum
1299 logical :: exist
1300 type(unit_t) :: fn_unit
1301 complex(real64) :: scal
1302
1303 push_sub(generate_wannier_states)
1304
1305 assert(st%d%ispin /= spinors)
1306
1307 message(1) = "oct-wannier90: Constructing the Wannier states from the U matrix."
1308 call messages_info(1)
1309
1310 inquire(file=trim(trim(adjustl(w90_prefix))//'_centres.xyz'),exist=exist)
1311 if (.not. exist) then
1312 message(1) = 'oct-wannier90: Cannot find the Wannier90 file seedname_centres.xyz.'
1313 write(message(2),'(a)') 'Please run wannier90.x with "write_xyz=.true." in '// trim(adjustl(w90_prefix)) // '.'
1314 call messages_fatal(2)
1315 end if
1316
1317 w90_xyz = io_open(trim(trim(adjustl(w90_prefix))//'_centres.xyz'), global_namespace, action='read')
1318
1319 safe_allocate(centers(1:3, 1:w90_num_wann))
1320 !Skip two lines
1321 read(w90_xyz, *)
1322 read(w90_xyz, *)
1323 do iw = 1, w90_num_wann
1324 read(w90_xyz, *) dum, centers(1:3, iw)
1325 ! Wannier90 outputs the coordinates in angstrom
1326 centers(1:3, iw) = units_to_atomic(unit_angstrom, centers(1:3, iw))
1327 end do
1328 call io_close(w90_xyz)
1329
1330
1331 inquire(file=trim(trim(adjustl(w90_prefix))//'_u_dis.mat'),exist=exist)
1332 if (exist) then
1333 message(1) = 'oct-wannier90: Calculation of Wannier states with disentanglement is not yet supported.'
1334 call messages_fatal(1)
1335 end if
1336
1337 inquire(file=trim(trim(adjustl(w90_prefix))//'_u.mat'),exist=exist)
1338 if (.not. exist) then
1339 message(1) = 'oct-wannier90: Cannot find the Wannier90 seedname_u.mat file.'
1340 write(message(2),'(a)') 'Please run wannier90.x with "write_u_matrices=.true." in '// trim(adjustl(w90_prefix)) // '.'
1341 call messages_fatal(2)
1342 end if
1343 w90_u_mat = io_open(trim(trim(adjustl(w90_prefix))//'_u.mat'), global_namespace, action='read')
1344
1345
1346 !Skip one line
1347 read(w90_u_mat, *)
1348 !Read num_kpts, num_wann, num_wann for consistency check
1349 read(w90_u_mat, *) nik, nwann, nwann
1350 if (nik /= w90_num_kpts .or. nwann /= w90_num_wann) then
1351 print *, w90_num_wann, w90_num_kpts, nik, nwann
1352 message(1) = "The file contains U matrices is inconsistent with the .win file."
1353 call messages_fatal(1)
1354 end if
1355
1356 safe_allocate(umnk(1:w90_num_wann, 1:w90_num_wann, 1:w90_num_kpts))
1357
1358 do ik = 1, w90_num_kpts
1359 !Skip one line
1360 read(w90_u_mat, *)
1361 !Skip one line (k-point coordinate)
1362 read(w90_u_mat, *)
1363 read(w90_u_mat, '(f15.10,sp,f15.10)') ((umnk(iw, iw2, ik), iw=1, w90_num_wann), iw2=1, w90_num_wann)
1364 end do
1365
1366 call io_close(w90_u_mat)
1367
1368 !We read the output format for the Wannier states
1369 call parse_variable(global_namespace, 'OutputFormat', 0, how)
1370
1371 call io_mkdir('wannier', global_namespace)
1372
1373 !Computing the Wannier states in the primitive cell, from the U matrices
1374 safe_allocate(zwn(1:mesh%np))
1375 safe_allocate(psi(1:mesh%np, 1:st%d%dim))
1376 safe_allocate(phase(1:mesh%np))
1377
1378 do iw = 1, w90_num_wann
1380 zwn(:) = m_z0
1381
1382 do ik = 1, w90_num_kpts
1383 kpoint(1:space%dim) = kpoints%get_point(ik, absolute_coordinates=.true.)
1384
1385 ! We compute the Wannier orbital on a grid centered around the Wannier function
1386 do ip = 1, mesh%np
1387 xx = mesh%x(ip, 1:space%dim)-centers(1:space%dim, iw)
1388 xx = ions%latt%fold_into_cell(xx)
1389 phase(ip) = exp(-m_zi* sum( xx * kpoint(1:space%dim)))
1390 end do
1391
1392 do iw2 = 1, st%nst
1393 if (exclude_list(iw2)) cycle
1394
1395 if (st%d%ispin /= spin_polarized) then
1396 call states_elec_get_state(st, mesh, iw2, ik, psi)
1397 else
1398 call states_elec_get_state(st, mesh, iw2, (ik-1)*2+w90_spin_channel, psi)
1399 end if
1400
1401 ! The minus sign is here is for the wrong convention of Octopus
1402 do ip = 1, mesh%np
1403 zwn(ip) = zwn(ip) + umnk(band_index(iw2), iw, ik) * psi(ip, 1) * phase(ip)
1404 end do
1405 end do!ik
1406 end do!iw2
1407
1408 ! Following what Wannier90 is doing, we fix the global phase by setting the max to be real
1409 ! We also normalize to the number of k-point at this step
1410 ipmax = 0
1411 wmodmax = m_zero
1412 do ip = 1, mesh%np
1413 wmod = real(zwn(ip)*conjg(zwn(ip)), real64)
1414 if (wmod > wmodmax) then
1415 ipmax = ip
1416 wmodmax = wmod
1417 end if
1418 end do
1419 scal = sqrt(wmodmax)/zwn(ipmax)/w90_num_kpts
1420 call mesh_minmaxloc(mesh, wmodmax, rankmax, mpi_maxloc)
1421 call mesh%mpi_grp%bcast(scal, 1, mpi_double_complex, rankmax)
1422 call lalg_scal(mesh%np, scal, zwn)
1423
1424 ! Output the Wannier states
1425 fn_unit = sqrt(units_out%length**(-space%dim))
1426 write(fname, '(a,i3.3)') 'wannier-', iw
1427 call zio_function_output(how, "wannier", trim(fname), global_namespace, space, mesh, &
1428 zwn, fn_unit, ierr, pos=ions%pos, atoms=ions%atom, grp = st%dom_st_kpt_mpi_grp)
1429
1430 ! Checking the ratio imag/real
1431 wmodmax = m_zero
1432 do ip = 1, mesh%np
1433 if(abs(real(zwn(ip), real64)) >= 1e-2_real64) then
1434 wmodmax = max(wmodmax, abs(aimag(zwn(ip)))/abs(real(zwn(ip), real64)))
1435 end if
1436 end do
1437 call mesh_minmaxloc(mesh, wmodmax, rankmax, mpi_maxloc)
1438
1439 write(message(1), '(a,i4,a,f11.6)') 'oct-wannier90: Wannier function ', iw, ' Max. Im/Re Ratio = ', wmodmax
1440 call messages_info(1)
1441 end do
1442
1443 safe_deallocate_a(umnk)
1444 safe_deallocate_a(zwn)
1445 safe_deallocate_a(psi)
1446 safe_deallocate_a(phase)
1447 safe_deallocate_a(centers)
1448
1450 end subroutine generate_wannier_states
1451
1452end program wannier90_interface
1453
1454!! Local Variables:
1455!! mode: f90
1456!! coding: utf-8
1457!! End:
double log(double __x) __attribute__((__nothrow__
double exp(double __x) __attribute__((__nothrow__
double sqrt(double __x) __attribute__((__nothrow__
This module implements batches of mesh functions.
Definition: batch.F90:135
integer, parameter, public batch_not_packed
functions are stored in CPU memory, unpacked order
Definition: batch.F90:286
integer, parameter, public batch_device_packed
functions are stored in device memory in packed order
Definition: batch.F90:286
integer, parameter, public batch_packed
functions are stored in CPU memory, in transposed (packed) order
Definition: batch.F90:286
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 zmesh_to_cube(mesh, mf, cube, cf)
Convert a function from the mesh to the cube.
subroutine, public dcube_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_init(cube, nn, namespace, space, spacing, coord_system, fft_type, fft_library, dont_optimize, nn_out, mpi_grp, need_partition, tp_enlarge, blocksize)
Definition: cube.F90:204
subroutine, public cube_end(cube)
Definition: cube.F90:387
subroutine, public cube_init_cube_map(cube, mesh)
Definition: cube.F90:824
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:280
subroutine, public fft_all_end()
delete all plans
Definition: fft.F90:391
real(real64), parameter, public m_two
Definition: global.F90:192
subroutine, public global_end()
Finalise parser varinfo file, and MPI.
Definition: global.F90:387
real(real64), parameter, public m_huge
Definition: global.F90:208
real(real64), parameter, public m_zero
Definition: global.F90:190
complex(real64), parameter, public m_z0
Definition: global.F90:200
complex(real64), parameter, public m_zi
Definition: global.F90:204
subroutine, public global_init(communicator)
Initialise Octopus.
Definition: global.F90:330
real(real64), parameter, public m_half
Definition: global.F90:196
real(real64), parameter, public m_one
Definition: global.F90:191
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:466
subroutine, public io_end()
Definition: io.F90:270
integer function, public io_open(file, namespace, action, status, form, position, die, recl, grp)
Definition: io.F90:401
subroutine, public kpoints_to_absolute(latt, kin, kout)
Definition: kpoints.F90:1033
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:279
subroutine, public messages_not_implemented(feature, namespace)
Definition: messages.F90:1097
subroutine, public messages_init(output_dir)
Definition: messages.F90:226
subroutine, public messages_warning(no_lines, all_nodes, namespace)
Definition: messages.F90:531
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:416
subroutine, public messages_experimental(name, namespace)
Definition: messages.F90:1069
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:600
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:134
subroutine, public orbitalset_init(this)
Definition: orbitalset.F90:205
subroutine, public orbitalset_end(this)
Definition: orbitalset.F90:231
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:281
logical function, public parse_is_defined(namespace, name)
Definition: parser.F90:505
subroutine, public parser_init()
Initialise the Octopus parser.
Definition: parser.F90:453
subroutine, public parser_end()
End the Octopus parser.
Definition: parser.F90:484
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:625
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:183
subroutine, public zstates_elec_rrqr_decomposition(st, namespace, mesh, nst, root, 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, 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:282
type(type_t), 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:220
Describes mesh distribution to nodes.
Definition: mesh.F90:188
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_eig()
subroutine read_wannier90_files()
subroutine create_wannier90_mmn(mesh, st)
subroutine wannier90_setup(ions, kpoints, space)
subroutine write_unk(space, mesh, st)
subroutine generate_wannier_states(space, mesh, ions, st, kpoints)
program wannier90_interface
subroutine create_wannier90_amn(space, mesh, latt, st, kpoints)
subroutine wannier90_output()