Octopus
wannier_calc.F90
Go to the documentation of this file.
1!! Copyright (C) 2017-2019 H. Huebener, N. Tancogne-Dejean
2!! Copyright (C) 2026 A. Buccheri, J. Reimann
3!!
4!! This program is free software; you can redistribute it and/or modify
5!! it under the terms of the GNU General Public License as published by
6!! the Free Software Foundation; either version 2, or (at your option)
7!! any later version.
8!!
9!! This program is distributed in the hope that it will be useful,
10!! but WITHOUT ANY WARRANTY; without even the implied warranty of
11!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12!! GNU General Public License for more details.
13!!
14!! You should have received a copy of the GNU General Public License
15!! along with this program; if not, write to the Free Software
16!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17!! 02110-1301, USA.
18!!
19
20#include "global.h"
21
24 use, intrinsic :: iso_fortran_env
25
27 use comm_oct_m
28 use debug_oct_m
30 use global_oct_m
32 use io_oct_m
34 use ions_oct_m
37 use mesh_oct_m
40 use mpi_oct_m
46 use space_oct_m
51 use types_oct_m
52 use unit_oct_m
55 use wfs_elec_oct_m, only: wfs_elec_t
57
58 implicit none
59 private
60
61 public :: &
75
81 integer :: l, m, s
82 integer :: radial = 1
83 real(real64) :: site(3)
84 real(real64) :: s_qaxis(3) = [0.0_real64, 0.0_real64, 1.0_real64]
85 real(real64) :: z(3) = [0.0_real64, 0.0_real64, 1.0_real64]
86 real(real64) :: x(3) = [1.0_real64, 0.0_real64, 0.0_real64]
87 real(real64) :: zona = 1.0_real64
88 end type wannier_calc_proj_t
89
90contains
91
96 subroutine wannier_calc_load_restart(namespace, mc, space, st, gr, kpoints, restart_states)
97 type(namespace_t), intent(in) :: namespace
98 type(multicomm_t), intent(in) :: mc
99 class(space_t), intent(inout) :: space
100 type(states_elec_t), intent(inout) :: st
101 class(mesh_t), intent(inout) :: gr
102 type(kpoints_t), intent(inout) :: kpoints
103 integer, optional, intent(in) :: restart_states ! RESTART_GS or RESTART_TD
104 ! Defaults to ground state
105
106 integer :: restart_states_, ierr, nik, dim, nst
107 type(restart_t) :: restart
108
109 restart_states_ = optional_default(restart_states, restart_gs)
110 if (.not. (restart_states_ == restart_gs .or. restart_states_ == restart_td)) then
111 message(1) = 'Invalid restart_states value; expected RESTART_GS or RESTART_TD.'
112 call messages_fatal(1)
113 end if
114
115 call states_elec_allocate_wfns(st, gr, wfs_type = type_cmplx)
116 call restart%init(namespace, restart_states_, restart_type_load, mc, ierr, gr)
117
118 if (ierr /= 0) then
119 write(message(1),'(a)') 'Error opening restart file'
120 call messages_fatal(1)
121 endif
122
123 call states_elec_look(restart, nik, dim, nst, ierr)
124 if (st%d%ispin == spin_polarized) nik = nik / 2
125 if (dim /= st%d%dim) then
126 write(message(1),'(a)') 'Restart structure not commensurate, since spin dimensions differ.'
127 call messages_fatal(1)
128 else if (nik /= kpoints%reduced%npoints) then
129 write(message(1),'(a)') 'Restart structure not commensurate, since number of k-points differ.'
130 call messages_fatal(1)
131 else if (nst /= st%nst) then
132 write(message(1),'(a)') 'Restart structure not commensurate, since number of states differ.'
133 call messages_fatal(1)
134 end if
135
136 call states_elec_load(restart, namespace, space, st, gr, kpoints, fixed_occ=st%restart_fixed_occ , &
137 ierr=ierr, label = ": wannier90")
138 call restart%end()
139
140 end subroutine wannier_calc_load_restart
141
142 ! TODO: Removed SCDM - Should reintroduce once the code is working
148 subroutine wannier_calc_create_amn(wan_opts, &
149 band_index, space, mesh, latt, st, kpoints, &
150 spin_channel, num_proj, proj_input, projection)
151 type(wannier_opts_t), intent(in) :: wan_opts
152 class(space_t), intent(in) :: space
153 class(mesh_t), intent(in) :: mesh
154 type(lattice_vectors_t), intent(in) :: latt
155 type(states_elec_t), intent(in) :: st
156 type(kpoints_t), intent(in) :: kpoints
157 integer, contiguous, intent(in) :: band_index(:)
158 integer, intent(in) :: spin_channel
159 integer, intent(in) :: num_proj
160 type(wannier_calc_proj_t), contiguous, intent(in) :: proj_input(:)
161
162 complex(real64), contiguous, intent(out) :: projection(:, :, :)
163
164 integer :: ist, ik, idim, iw, ip, ik_real, iband, num_kpts
165 real(real64) :: center(3), kpoint(3)
166
167 complex(real64), allocatable :: psi(:,:), phase(:)
168 real(real64), allocatable :: ylm(:)
169 type(orbitalset_t), allocatable :: orbitals(:)
170
172 call profiling_in("W90_AMN")
173
174 if (st%parallel_in_states) then
175 call messages_not_implemented("w90_amn output with states parallelization")
176 end if
178 message(1) = "Info: Computing the projection matrix"
181 assert(num_proj == size(proj_input))
183 num_kpts = kpoints%full%npoints
184
185 ! Precompute orbitals
186 safe_allocate(orbitals(1:num_proj))
187 do iw=1, num_proj
188 call orbitalset_init(orbitals(iw))
189
190 orbitals(iw)%norbs = 1
191 orbitals(iw)%ndim = 1
192 orbitals(iw)%radius = -log(wan_opts%threshold)
193 orbitals(iw)%use_submesh = .false.
194
195 ! cartesian coordinate of orbital center
196 center(1:3) = latt%red_to_cart(proj_input(iw)%site(1:3))
197 call submesh_init(orbitals(iw)%sphere, space, mesh, latt, center, orbitals(iw)%radius)
198
199 ! get dorb as submesh points
200 safe_allocate(ylm(1:orbitals(iw)%sphere%np))
201 ! (this is a routine from pwscf)
202 call ylm_wannier(ylm, proj_input(iw)%l, proj_input(iw)%m, &
203 orbitals(iw)%sphere%r, orbitals(iw)%sphere%rel_x, orbitals(iw)%sphere%np)
204
205 if (proj_input(iw)%radial /= 1) then
206 call messages_not_implemented("oct-wannier90: r/=1 for the radial part")
207 end if
208
209 ! apply radial function
210 do ip = 1, orbitals(iw)%sphere%np
211 ylm(ip) = ylm(ip) * m_two * exp(-orbitals(iw)%sphere%r(ip))
212 end do
213
214 ! NOTE(Alex) One can probably fill directly into zorb, and avoid allocating ylm
215 safe_allocate(orbitals(iw)%zorb(1:orbitals(iw)%sphere%np, 1:1, 1:1))
216 orbitals(iw)%zorb(1:orbitals(iw)%sphere%np, 1, 1) = ylm(1:orbitals(iw)%sphere%np)
217 safe_deallocate_a(ylm)
218
219 safe_allocate(orbitals(iw)%phase(1:orbitals(iw)%sphere%np, st%d%kpt%start:st%d%kpt%end))
220 orbitals(iw)%phase = m_z0
221 safe_allocate(orbitals(iw)%eorb_mesh(1:mesh%np, 1:1, 1:1, st%d%kpt%start:st%d%kpt%end))
222 orbitals(iw)%eorb_mesh = m_z0
223
224 call orbitalset_update_phase(orbitals(iw), space%dim, st%d%kpt, kpoints, st%d%ispin == spin_polarized, &
225 kpt_max = num_kpts)
226 end do
227
228 ! Compute the projections
229 safe_allocate(psi(1:mesh%np, 1:st%d%dim))
230 safe_allocate(phase(1:mesh%np))
231 projection = m_zero
232
233 do ik = 1, num_kpts
234 kpoint(1:space%dim) = kpoints%get_point(ik)
235 do ip = 1, mesh%np
236 phase(ip) = exp(-m_zi * sum(mesh%x(1:space%dim, ip) * kpoint(1:space%dim)))
237 end do
238
239 ik_real = st%d%get_k_spin_index(ik, spin_channel)
240
241 ! TODO(Alex) ist and iw loops need interchanging to optimise array access
242 do ist = 1, size(band_index)
243 iband = band_index(ist)
244
245 if(ik_real >= st%d%kpt%start .and. ik_real <= st%d%kpt%end) then
246 call states_elec_get_state(st, mesh, iband, ik_real, psi)
247
248 do idim = 1, st%d%dim
249 do ip = 1, mesh%np
250 psi(ip, idim) = psi(ip, idim)*phase(ip)
251 end do
252 end do
253
254 do iw = 1, num_proj
255 idim = 1
256 if (wan_opts%spinors) idim = wan_opts%spin_proj_component(iw)
257 !At the moment the orbitals do not depend on idim
258 !The idim index for eorb_mesh would be for a spin-resolved orbital like j=1/2
259 projection(iband, iw, ik) = zmf_dotp(mesh, psi(1:mesh%np, idim), &
260 orbitals(iw)%eorb_mesh(1:mesh%np, 1, 1, ik_real), reduce = .false.)
261 end do
262
263 call profiling_in("W90_AMN_REDUCE")
264 call mesh%allreduce(projection(ist, :, ik))
265 call profiling_out("W90_AMN_REDUCE")
266 end if
267
268 if(st%d%kpt%parallel) then
269 call comm_allreduce(st%d%kpt%mpi_grp, projection(ist, :, ik))
270 end if
271
272 end do !ist
273 end do !ik
274
275 safe_deallocate_a(psi)
276 safe_deallocate_a(phase)
277
278 do iw = 1, num_proj
279 call orbitalset_end(orbitals(iw))
280 end do
281 safe_deallocate_a(orbitals)
282
283 call profiling_out("W90_AMN")
284
286
287 end subroutine wannier_calc_create_amn
288
298 subroutine wannier_calc_create_mmn(band_index, mesh, st, latt, &
299 spin_channel, nnlist, nncell, overlap)
300 integer, contiguous, intent(in) :: band_index(:)
301 class(mesh_t), intent(in) :: mesh
302 type(states_elec_t), target, intent(in) :: st
303 type(lattice_vectors_t), intent(in) :: latt ! can differ from ions%latt for non-periodic systems
304 integer, intent(in) :: spin_channel
305 integer, contiguous, intent(in) :: nnlist(:,:)
306 integer, contiguous, intent(in) :: nncell(:,:,:)
307
308 complex(real64), contiguous, intent(out) :: overlap(:, :, :, :) ! distributed in k-points
309
310 integer :: ist, jst, ik, ip, iknn, idim, ibind
311 integer :: ik_oct, iknn_oct, inn
312 integer :: ik_loc
313 integer :: iband, jband, inode, node_fr, node_to
314 real(real64) :: gcart(3)
315 integer :: g(3)
316
317 integer :: nntot, num_kpts
318
319 complex(real64), allocatable :: psim(:,:), psin(:,:), phase(:)
320 type(wfs_elec_t), pointer :: batch
321 type(mpi_request) :: send_req
322
324
325 call profiling_in("W90_MMN")
326
327
328 nntot = size(nnlist, 2)
329 num_kpts = size(nnlist, 1)
330 assert(size(nnlist, 1) == num_kpts)
331 assert(size(nncell, 1) == 3)
332 assert(size(nncell, 2) == num_kpts)
333 assert(size(nncell, 3) == nntot)
334
335 if (st%parallel_in_states) then
336 call messages_not_implemented("w90_mmn output with states parallelization")
337 end if
338
339 message(1) = "Info: Computing the overlap matrix for wannierisation"
340 call messages_info(1)
341
342 overlap = m_zero
343 safe_allocate(psim(1:mesh%np, 1:st%d%dim))
344 safe_allocate(psin(1:mesh%np, 1:st%d%dim))
345 safe_allocate(phase(1:mesh%np))
346
347 ! overlap is distributed in k like wannier90''s m_matrix_local
348 ik_loc = 0
349
350 ! loop over the pairs specified in the nnk tuple
351 do ik = 1, num_kpts
352 ik_oct = st%d%get_k_spin_index(ik, spin_channel)
353 if (ik_oct >= st%d%kpt%start .and. ik_oct <= st%d%kpt%end) ik_loc = ik_loc + 1
354
355 do inn = 1, nntot
356
357 iknn = nnlist(ik, inn)
358 g(1:3) = nncell(:, ik, inn)
359 iknn_oct = st%d%get_k_spin_index(iknn, spin_channel)
360
361 ! If the k-point is local, compute the phase e^{-i G.r}
362 if(ik_oct >= st%d%kpt%start .and. ik_oct <= st%d%kpt%end) then
363 ! Wannier90 treats everything fully periodic
364 ! Conversion is done with the 3D "correct" klattice
365 call kpoints_to_absolute(latt, real(g, real64) , gcart)
366
367 ! Phase that gives u_{n,k+G}(r) from u_{nk}(r)
368 ! Here the minus sign of Octopus cancels with minus sign of the input G
369 ! (ik and iknn correspond in the list to minus the k-points in Octopus)
370 if (any(g /= 0)) then
371 do ip = 1, mesh%np
372 phase(ip) = exp(-m_zi*dot_product(mesh%x(1:3, ip), gcart(1:3)))
373 end do
374 end if
375 end if
376
377 ! Loop over distributed bands
378 do jst = 1, size(band_index)
379 jband = band_index(jst)
380
381 if (st%d%kpt%parallel) then
382 node_fr = -1
383 node_to = -1
384 do inode = 0, st%d%kpt%mpi_grp%size-1
385 if(iknn_oct >= st%st_kpt_task(inode,3) .and. iknn_oct <= st%st_kpt_task(inode,4)) then
386 node_fr = inode
387 end if
388 if(ik_oct >= st%st_kpt_task(inode,3) .and. ik_oct <= st%st_kpt_task(inode,4)) then
389 node_to = inode
390 end if
391 end do
392 assert(node_fr > -1)
393 assert(node_to > -1)
394 send_req = mpi_request_null
395 ! We have locally the k-point
396 if (state_kpt_is_local(st, jband, iknn_oct)) then
397 call states_elec_get_state(st, mesh, jband, iknn_oct, psin)
398 ! We send it only if we don`t want to use it locally
399 if(node_to /= st%d%kpt%mpi_grp%rank) then
400 call st%d%kpt%mpi_grp%isend(psin, mesh%np*st%d%dim, mpi_double_complex, node_to, send_req)
401 end if
402 end if
403 ! We receive the desired state, only if it is not a local one
404 if(node_to == st%d%kpt%mpi_grp%rank .and. node_to /= node_fr) then
405 call st%d%kpt%mpi_grp%recv(psin, mesh%np*st%d%dim, mpi_double_complex, node_fr)
406 end if
407 if (send_req /= mpi_request_null) then
408 call st%d%kpt%mpi_grp%wait(send_req)
409 end if
410 else
411 call states_elec_get_state(st, mesh, jband, iknn_oct, psin)
412 end if
413
414 if(ik_oct >= st%d%kpt%start .and. ik_oct <= st%d%kpt%end) then
415
416 ! Do not apply the phase if the phase factor is 1
417 if (any(g /= 0)) then
418 do idim = 1, st%d%dim
419 do ip = 1, mesh%np
420 psin(ip, idim) = psin(ip, idim) * phase(ip)
421 end do
422 end do
423 end if
424
425 ! See Eq. (25) in PRB 56, 12847 (1997)
426 ! Loop over local k-points
427 do iband = 1, size(band_index)
428 ist = band_index(iband)
429
430 batch => st%group%psib(st%group%iblock(ist), ik_oct)
431
432 select case (batch%status())
433 case (batch_not_packed)
434 overlap(iband, jst, inn, ik_loc) = m_z0
435 do idim = 1, st%d%dim
436 ibind = batch%inv_index((/ist, idim/))
437 overlap(iband, jst, inn, ik_loc) = overlap(iband, jst, inn, ik_loc) + &
438 zmf_dotp(mesh, batch%zff_linear(:, ibind), psin(:,idim), reduce = .false.)
439 end do
440 !Not properly done at the moment
442 call states_elec_get_state(st, mesh, ist, ik_oct, psim)
443 overlap(iband, jst, inn, ik_loc) = zmf_dotp(mesh, st%d%dim, psim, psin, reduce = .false.)
444 end select
445 end do !iband
446
447 call profiling_in("W90_MMN_REDUCE")
448 call mesh%allreduce(overlap(:, jst, inn, ik_loc))
449 call profiling_out("W90_MMN_REDUCE")
450 end if
451 end do !jst
452 end do !inn
453 end do !ik
454
455 assert(ik_loc == size(overlap, 4))
456
457 safe_deallocate_a(psim)
458 safe_deallocate_a(psin)
459 safe_deallocate_a(phase)
460
461 call profiling_out("W90_MMN")
462
464
465 end subroutine wannier_calc_create_mmn
466
471 subroutine wannier_calc_read_centers(wan_opts, centers)
472 type(wannier_opts_t), intent(in) :: wan_opts
473 real(real64), contiguous, intent(inout) :: centers(:, :)
474
475 integer :: w90_xyz, iw
476 character(len=2) :: dum
477 logical :: exist
478
480
481 assert(size(centers, 2) == wan_opts%num_wann)
482 assert(size(centers, 1) == 3)
483
484 inquire(file=trim(adjustl(wan_opts%prefix))//'_centres.xyz',exist=exist)
485 if (.not. exist) then
486 message(1) = 'Cannot find the Wannier90 file seedname_centres.xyz.'
487 call messages_fatal(1)
488 end if
489
490 w90_xyz = io_open(trim(adjustl(wan_opts%prefix))//'_centres.xyz', global_namespace, action='read')
491
492 !Skip two lines
493 read(w90_xyz, *)
494 read(w90_xyz, *)
495 do iw = 1, wan_opts%num_wann
496 read(w90_xyz, *) dum, centers(1:3, iw)
497 ! Wannier90 outputs the coordinates in angstrom
498 centers(1:3, iw) = units_to_atomic(unit_angstrom, centers(1:3, iw))
499 end do
500 call io_close(w90_xyz)
501
503 end subroutine wannier_calc_read_centers
504
509 subroutine wannier_calc_read_u_matrices(wan_opts, kpoints, u_matrix, u_dis_matrix)
510 type(wannier_opts_t), intent(in) :: wan_opts
511 type(kpoints_t), intent(in) :: kpoints
512 complex(real64), contiguous, intent(inout) :: u_matrix(:, :, :)
513 complex(real64), contiguous, intent(inout) :: u_dis_matrix(:, :, :)
514
515 integer :: w90_u_mat, w90_u_dis, ik, ib, iw, iw2, nik, nwann, nib
516 integer :: num_kpts
517 logical :: exist, have_disentangled
518
519 num_kpts = product(kpoints%nik_axis(1:3))
520
522
523 inquire(file=trim(adjustl(wan_opts%prefix))//'_u.mat',exist=exist)
524 if (.not. exist) then
525 message(1) = 'Cannot find the Wannier90 seedname_u.mat file.'
526 call messages_fatal(1)
527 end if
528 w90_u_mat = io_open(trim(adjustl(wan_opts%prefix))//'_u.mat', global_namespace, action='read')
529
530 !Skip one line
531 read(w90_u_mat, *)
532 !Read num_kpts, num_wann, num_wann for consistency check
533 read(w90_u_mat, *) nik, nwann, nwann
534 if (nik /= num_kpts .or. nwann /= wan_opts%num_wann) then
535 message(1) = "The U matrix has inconsistent shape."
536 call messages_fatal(1)
537 end if
538
539 do ik = 1, num_kpts
540 !Skip one line
541 read(w90_u_mat, *)
542 !Skip one line (k-point coordinate)
543 read(w90_u_mat, *)
544 read(w90_u_mat, '(f15.10,sp,f15.10)') ((u_matrix(iw, iw2, ik), iw=1, wan_opts%num_wann), iw2=1, wan_opts%num_wann)
545 end do
546
547 call io_close(w90_u_mat)
548
549 inquire(file=trim(adjustl(wan_opts%prefix))//'_u_dis.mat',exist=have_disentangled)
550 if (have_disentangled) then
551 ! Read u_dis.mat: dimensions are (w90_num_bands x w90_num_wann) per k-point.
552 ! The band rows follow the compressed, non-excluded band ordering used by
553 ! the rest of the Wannier90 interface (i.e. after applying exclude_bands).
554 w90_u_dis = io_open(trim(adjustl(wan_opts%prefix))//'_u_dis.mat', global_namespace, action='read')
555 !Skip one line
556 read(w90_u_dis, *)
557 !Read num_kpts, num_wann, num_bands for consistency check
558 read(w90_u_dis, *) nik, nwann, nib
559 if (nik /= num_kpts .or. nwann /= wan_opts%num_wann .or. nib /= wan_opts%num_bands) then
560 message(1) = 'The u_dis matrix has inconsistent shape.'
561 call messages_fatal(1)
562 end if
563
564 do ik = 1, num_kpts
565 !Skip one line
566 read(w90_u_dis, *)
567 !Skip one line (k-point coordinate)
568 read(w90_u_dis, *)
569 read(w90_u_dis, '(f15.10,sp,f15.10)') ((u_dis_matrix(ib, iw, ik), ib=1, wan_opts%num_bands), iw=1, wan_opts%num_wann)
570 end do
571
572 call io_close(w90_u_dis)
573
574 ! In case of disentanglement, u_dis matrix needs to be reordered to the correct layout.
575 ! See issue #1465.
576 end if
577
579 end subroutine wannier_calc_read_u_matrices
580
584 subroutine wannier_calc_get_header(header_start, header)
585 character(len=*), intent(in) :: header_start
586 character(len=*), intent(out) :: header
587
588 integer :: date_time(8)
589
591
592 call date_and_time(values=date_time)
593 write(header, '(a,i4,a1,i2.2,a1,i2.2,a,i2.2,a1,i2.2,a1,i2.2)') &
594 header_start, date_time(1), '/', date_time(2), '/', date_time(3), &
595 ' at ', date_time(5), ':', date_time(6), ':', date_time(7)
596
598 end subroutine wannier_calc_get_header
599
603 subroutine wannier_calc_write_centers(dir, prefix, centers, ions)
604 character(len=*), intent(in) :: dir
605 character(len=*), intent(in) :: prefix
606 real(real64), contiguous, intent(in) :: centers(:, :)
607 class(ions_t), intent(in) :: ions
608
609 integer :: w90_xyz, iw, ia, ind
610 character(len=512) :: header
611
613
614 w90_xyz = io_open(trim(adjustl(dir))//'/'//trim(adjustl(prefix))//'_centres.xyz', global_namespace, action='write', die=.false.)
615 if (w90_xyz == -1) then
616 message(1) = 'Unable to open output file '//trim(adjustl(prefix))//'_centres.xyz for writing.'
617 call messages_fatal(1)
618 end if
619
620 write(w90_xyz, '(i6)') size(centers, 2) + ions%natoms
621 call wannier_calc_get_header('Wannier centers, written by octopus on ', header)
622 write (w90_xyz, *) trim(header)
623
624 do iw = 1, size(centers, 2)
625 write(w90_xyz, '("X",6x,3(f14.8,3x))') &
626 (units_from_atomic(unit_angstrom, centers(ind, iw)), ind=1, 3)
627 end do
628 do ia = 1, ions%natoms
629 write(w90_xyz, '(a2,5x,3(f14.8,3x))') trim(ions%atom(ia)%label), &
630 (units_from_atomic(unit_angstrom, ions%pos(ind, ia)), ind=1, 3)
631 end do
632 call io_close(w90_xyz)
633
635
636 end subroutine wannier_calc_write_centers
637
642 subroutine wannier_calc_write_u_matrices(dir, prefix, kpoints, u_matrix, u_dis_matrix)
643 character(len=*), intent(in) :: dir
644 character(len=*), intent(in) :: prefix
645 type(kpoints_t), intent(in) :: kpoints
646 complex(real64), contiguous, intent(in) :: u_matrix(:, :, :)
647 complex(real64), contiguous, intent(in) :: u_dis_matrix(:, :, :)
648
649 integer :: w90_u_mat, w90_u_dis
650 logical :: have_disentangled
651 integer :: i, j, nkp, num_wann, num_bands, num_kpts
652 character(len=512) :: header
653
654 num_kpts = size(u_matrix, 3)
655 num_wann = size(u_matrix, 1)
656 num_bands = size(u_dis_matrix, 1)
657 assert(num_kpts == size(kpoints%reduced%red_point, 2))
658
660
661 call wannier_calc_get_header('written on ', header)
662
663 w90_u_mat = io_open(trim(adjustl(dir))//'/'//trim(adjustl(prefix))//'_u.mat', global_namespace, action='write', die=.false.)
664 if (w90_u_mat == -1) then
665 message(1) = 'Unable to open output file '//trim(adjustl(prefix))//'_u.mat for writing.'
666 call messages_fatal(1)
667 end if
668
669 write (w90_u_mat, *) trim(header)
670 write (w90_u_mat, *) num_kpts, num_wann, num_wann
671
672 do nkp = 1, num_kpts
673 write (w90_u_mat, *)
674 write (w90_u_mat, '(f15.10,sp,f15.10,sp,f15.10)') -kpoints%reduced%red_point(1:3, nkp)
675 write (w90_u_mat, '(f15.10,sp,f15.10)') ((u_matrix(i, j, nkp), i=1, num_wann), j=1, num_wann)
676 end do
677
678 call io_close(w90_u_mat)
680 inquire(file=trim(trim(adjustl(prefix))//'_u_dis.mat'),exist=have_disentangled)
681 if (have_disentangled) then
682
683 w90_u_dis = io_open(trim(adjustl(dir))//'/'//trim(adjustl(prefix))//'_u_dis.mat', &
684 global_namespace, action='write', die=.false.)
685 if (w90_u_dis == -1) then
686 message(1) = 'Unable to open output file '//trim(adjustl(prefix))//'_u_dis.mat for writing.'
687 call messages_fatal(1)
688 end if
689
690 write (w90_u_dis, *) trim(header)
691 write (w90_u_dis, *) num_kpts, num_wann, num_bands
692 do nkp = 1, num_kpts
693 write (w90_u_dis, *)
694 write (w90_u_dis, '(f15.10,sp,f15.10,sp,f15.10)') -kpoints%reduced%red_point(1:3, nkp)
695 write (w90_u_dis, '(f15.10,sp,f15.10)') ((u_dis_matrix(i, j, nkp), i=1, num_bands), j=1, num_wann)
696 end do
697
698 call io_close(w90_u_dis)
699 end if
700
702
703 end subroutine wannier_calc_write_u_matrices
704
706 subroutine wannier_calc_write_spreads(dir, prefix, spreads)
707 character(len=*), intent(in) :: dir
708 character(len=*), intent(in) :: prefix
709 real(real64), contiguous, intent(in) :: spreads(:)
710
711 integer :: w90_xyz, iw
712 character(len=512) :: header
713
715
716 w90_xyz = io_open(trim(adjustl(dir))//'/'//trim(adjustl(prefix))//'_spreads.xyz', global_namespace, action='write', die=.false.)
717 if (w90_xyz == -1) then
718 message(1) = 'Unable to open output file '//trim(adjustl(prefix))//'_spreads.xyz for writing.'
719 call messages_fatal(1)
720 end if
721
722 call wannier_calc_get_header('Wannier spreads, written by octopus on ', header)
723 write (w90_xyz, *) trim(header)
724
725 do iw = 1, size(spreads, 1)
726 write(w90_xyz, '("X",6x,f14.8)') spreads(iw) * units_from_atomic(unit_angstrom, 1.0_real64)**2
727 end do
728 call io_close(w90_xyz)
729
731
732 end subroutine wannier_calc_write_spreads
733
739 subroutine wannier_calc_write_hr_file(dir, prefix, irvec, ndegen, ham_r)
740 character(len=*), intent(in) :: dir
741 character(len=*), intent(in) :: prefix
742 integer, contiguous, intent(in) :: irvec(:, :)
743 integer, contiguous, intent(in) :: ndegen(:)
744 complex(real64), contiguous, intent(in) :: ham_r(:, :, :)
745
746 integer :: w90_hr_dat
747 integer :: i, j, irpt
748 character(len=512) :: header
749 integer :: nrpts, n_wannier
750
752
753 nrpts = size(ham_r, 3)
754 n_wannier = size(ham_r, 1)
755 assert(size(irvec, 2) == nrpts)
756 assert(size(ndegen, 1) == nrpts)
757
758 w90_hr_dat = io_open(trim(adjustl(dir))//'/'//trim(adjustl(prefix))//'_hr.dat', global_namespace, action='write', die=.false.)
759 if (w90_hr_dat == -1) then
760 message(1) = 'Unable to open output file '//trim(adjustl(prefix))//'_hr.dat for writing.'
761 call messages_fatal(1)
762 end if
763
764 call wannier_calc_get_header('written on ', header)
765 write (w90_hr_dat, *) trim(header)
766
767 write (w90_hr_dat, *) size(ham_r, 1)
768 write (w90_hr_dat, *) nrpts
769 write (w90_hr_dat, '(15I5)') (ndegen(i), i=1, nrpts)
770 do irpt = 1, nrpts
771 do i = 1, n_wannier
772 do j = 1, n_wannier
773 write (w90_hr_dat, '(5I5,2F12.6)') irvec(:, irpt), j, i, &
774 units_from_atomic(unit_ev, ham_r(j, i, irpt))
775 end do
776 end do
777 end do
778
779 call io_close(w90_hr_dat)
780
782 end subroutine wannier_calc_write_hr_file
783
785 logical function wannier_calc_r_in_wigner_seitz_cell(rvec, real_metric, mp_grid, ws_search_size, &
786 ws_distance_tol, ndegen) result(in_cell)
787 integer, intent(in) :: rvec(3)
788 real(real64), intent(in) :: real_metric(3, 3)
789 integer, intent(in) :: mp_grid(3)
790 integer, intent(in) :: ws_search_size(3)
791 real(real64), intent(in) :: ws_distance_tol
792 integer, optional, intent(out) :: ndegen
793
794 integer :: i1, i2, i3, icnt
795 integer :: ndiff(3)
796 real(real64) :: dist_min, dist_to_origin
797 real(real64) :: dist(product(2*ws_search_size + 3))
798
799 ! no push_sub, called too frequently
800
801 ! Squared distances from rvec to all superlattice points within the search range
802 icnt = 0
803 do i1 = -ws_search_size(1) - 1, ws_search_size(1) + 1
804 do i2 = -ws_search_size(2) - 1, ws_search_size(2) + 1
805 do i3 = -ws_search_size(3) - 1, ws_search_size(3) + 1
806 icnt = icnt + 1
807 ndiff = rvec - [i1, i2, i3] * mp_grid
808 dist(icnt) = dot_product(real(ndiff, real64), matmul(real_metric, real(ndiff, real64)))
809 end do
810 end do
811 end do
812
813 dist_to_origin = dot_product(real(rvec, real64), matmul(real_metric, real(rvec, real64)))
814 dist_min = minval(dist)
815
816 in_cell = abs(dist_to_origin - dist_min) < ws_distance_tol**2
817 if (present(ndegen)) ndegen = count(abs(dist - dist_min) < ws_distance_tol**2)
818
820
825 subroutine wannier_calc_wigner_seitz_count(real_lattice, mp_grid, ws_search_size, ws_distance_tol, nrpts)
826 real(real64), intent(in) :: real_lattice(3,3)
827 integer, intent(in) :: mp_grid(3)
828 integer, intent(in) :: ws_search_size(3)
829 real(real64), intent(in) :: ws_distance_tol
830 integer, intent(out) :: nrpts
831
832 integer :: n1, n2, n3
833 real(real64) :: real_metric(3, 3)
836
837 real_metric = matmul(transpose(real_lattice), real_lattice)
838
839 nrpts = 0
840 do n1 = -ws_search_size(1)*mp_grid(1), ws_search_size(1)*mp_grid(1)
841 do n2 = -ws_search_size(2)*mp_grid(2), ws_search_size(2)*mp_grid(2)
842 do n3 = -ws_search_size(3)*mp_grid(3), ws_search_size(3)*mp_grid(3)
843 if (wannier_calc_r_in_wigner_seitz_cell([n1, n2, n3], real_metric, mp_grid, &
844 ws_search_size, ws_distance_tol)) then
845 nrpts = nrpts + 1
846 end if
847 end do
848 end do
849 end do
850
853
858 subroutine wannier_calc_wigner_seitz_fill(real_lattice, mp_grid, ws_search_size, ws_distance_tol, &
859 nrpts, irvec, ndegen)
860 real(real64), intent(in) :: real_lattice(3,3)
861 integer, intent(in) :: mp_grid(3)
862 integer, intent(in) :: ws_search_size(3)
863 real(real64), intent(in) :: ws_distance_tol
864 integer, intent(in) :: nrpts
865 integer, intent(out) :: irvec(3, nrpts)
866 integer, intent(out) :: ndegen(nrpts)
867
868 integer :: n1, n2, n3, irpt, ndeg
869 real(real64) :: real_metric(3, 3)
870
872
873 real_metric = matmul(transpose(real_lattice), real_lattice)
874
875 irpt = 0
876 do n1 = -ws_search_size(1)*mp_grid(1), ws_search_size(1)*mp_grid(1)
877 do n2 = -ws_search_size(2)*mp_grid(2), ws_search_size(2)*mp_grid(2)
878 do n3 = -ws_search_size(3)*mp_grid(3), ws_search_size(3)*mp_grid(3)
879 if (wannier_calc_r_in_wigner_seitz_cell([n1, n2, n3], real_metric, mp_grid, &
880 ws_search_size, ws_distance_tol, ndeg)) then
881 irpt = irpt + 1
882 irvec(:, irpt) = [n1, n2, n3]
883 ndegen(irpt) = ndeg
884 end if
885 end do
886 end do
887 end do
888 assert(irpt == nrpts)
889
890 ! Sum rule from wannier90: the R-points weighted by their inverse degeneracy
891 ! must add up to the number of k-points
892 if (abs(sum(m_one/ndegen) - product(mp_grid)) > 1e-8_real64) then
893 message(1) = "Error computing the Wigner-Seitz points."
894 call messages_fatal(1)
895 end if
896
898 end subroutine wannier_calc_wigner_seitz_fill
899
907 subroutine wannier_calc_compute_ham_r(namespace, mesh, st, hm, kpoints, wannier_opts, &
908 u_matrix, irvec, ham_r)
909 type(namespace_t), intent(in) :: namespace
910 class(mesh_t), intent(in) :: mesh
911 type(states_elec_t), target, intent(inout) :: st
912 type(hamiltonian_elec_t), intent(in) :: hm
913 type(kpoints_t), intent(in) :: kpoints
914 type(wannier_opts_t), intent(in) :: wannier_opts
915 complex(real64), contiguous, intent(in) :: u_matrix(:, :, :)
916 integer, contiguous, intent(in) :: irvec(:, :)
917 complex(real64), contiguous, intent(out) :: ham_r(:, :, :)
918
919 integer :: nk, nrpts, ik, irpt, iq, ikl
920 real(real64) :: rdotk
921 complex(real64), allocatable :: hmss(:, :), ham_k_local(:, :, :)
922
924
925 nk = kpoints%full%npoints
926 nrpts = size(ham_r, 3)
927
928 safe_allocate(hmss(1:wannier_opts%num_bands, 1:wannier_opts%num_bands))
929 safe_allocate(ham_k_local(1:wannier_opts%num_wann, 1:wannier_opts%num_wann, 1:st%d%kpt%nlocal()))
930
931 assert(wannier_opts%num_bands == st%nst)
932 if (wannier_opts%num_bands /= wannier_opts%num_wann) then
933 call messages_not_implemented('TD Wannier: num_bands unequal num_wann', namespace)
934 end if
935
936 ! Hamiltonian matrix elements in the subspace of the (time-dependent) KS states,
937 ! rotated into the Wannier basis: H_W(k) = U(k)^dagger eps(k) U(k)
938 ! Computed once per local k-point and cached
939 do iq = st%d%kpt%start, st%d%kpt%end
940 ik = st%d%get_kpoint_index(iq)
941 ikl = iq - st%d%kpt%start + 1
942 call zsubspace_diag_hamiltonian(namespace, mesh, st, hm, iq, hmss)
943 ham_k_local(:, :, ikl) = matmul(transpose(conjg(u_matrix(:, :, ik))), matmul(hmss, u_matrix(:, :, ik)))
944 end do
945
946 ! Adapted from Wannier90: Fourier transform to real space: H(R) = (1/N_k) sum_k exp(-i k.R) H_W(k)
947 ! Each rank sums only its own local k-points
948 ham_r = m_z0
949 do irpt = 1, nrpts
950 do iq = st%d%kpt%start, st%d%kpt%end
951 ik = st%d%get_kpoint_index(iq)
952 ikl = iq - st%d%kpt%start + 1
953 rdotk = m_two*m_pi*dot_product(kpoints%reduced%red_point(1:3, ik), real(irvec(1:3, irpt), real64))
954 ham_r(:, :, irpt) = ham_r(:, :, irpt) + (exp(m_zi*rdotk)/real(nk, real64)) * ham_k_local(:, :, ikl)
955 ! sign mismatch here because of mismatch in k-point convention between Octopus and Wannier90
956 end do
957 end do
958
959 if (st%d%kpt%parallel) then
960 call comm_allreduce(st%d%kpt%mpi_grp, ham_r)
961 end if
962
963 safe_deallocate_a(hmss)
964 safe_deallocate_a(ham_k_local)
965
967 end subroutine wannier_calc_compute_ham_r
968
969end module wannier_calc_oct_m
double log(double __x) __attribute__((__nothrow__
double exp(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:311
integer, parameter, public batch_device_packed
functions are stored in device memory in packed order
Definition: batch.F90:311
integer, parameter, public batch_packed
functions are stored in CPU memory, in transposed (packed) order
Definition: batch.F90:311
integer, parameter, public spin_polarized
real(real64), parameter, public m_two
Definition: global.F90:202
real(real64), parameter, public m_zero
Definition: global.F90:200
real(real64), parameter, public m_pi
some mathematical constants
Definition: global.F90:198
complex(real64), parameter, public m_z0
Definition: global.F90:210
complex(real64), parameter, public m_zi
Definition: global.F90:214
real(real64), parameter, public m_one
Definition: global.F90:201
subroutine, public write_standard_xyz_file(namespace, fname, pos, species, header)
Write a standard xyz file with atom labels and positions (in Angstrom).
Definition: io.F90:116
subroutine, public io_close(iunit, grp)
Definition: io.F90:467
integer function, public io_open(file, namespace, action, status, form, position, die, recl, grp)
Definition: io.F90:402
subroutine, public kpoints_to_absolute(latt, kin, kout)
Definition: kpoints.F90:1147
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_not_implemented(feature, namespace)
Definition: messages.F90:1068
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:162
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:410
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:594
This module handles the communicators for the various parallelization strategies.
Definition: multicomm.F90:147
type(namespace_t), public global_namespace
Definition: namespace.F90:135
subroutine, public orbitalset_init(this)
Definition: orbitalset.F90:209
subroutine, public orbitalset_end(this)
Definition: orbitalset.F90:235
subroutine, public orbitalset_update_phase(os, dim, kpt, kpoints, spin_polarized, vec_pot, vec_pot_var, kpt_max)
Build the phase correction to the global phase in case the orbital crosses the border of the simulato...
Definition: orbitalset.F90:285
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
Definition: profiling.F90:668
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
Definition: profiling.F90:600
integer, parameter, public restart_gs
Definition: restart.F90:156
integer, parameter, public restart_td
Definition: restart.F90:156
integer, parameter, public restart_type_load
Definition: restart.F90:184
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:232
subroutine, public zsubspace_diag_hamiltonian(namespace, mesh, st, hm, ik, hmss, overlap)
Diagonalises the Hamiltonian in the subspace defined by the states.
Definition: subspace.F90:978
type(type_t), parameter, public type_cmplx
Definition: types.F90:137
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.
type(unit_t), public unit_ev
For output energies in eV.
Wannier90 related calculations.
subroutine, public wannier_calc_write_centers(dir, prefix, centers, ions)
Write wannier centers to file.
subroutine, public wannier_calc_wigner_seitz_fill(real_lattice, mp_grid, ws_search_size, ws_distance_tol, nrpts, irvec, ndegen)
Calculate coordinates and degeneracies of R-points in the Wigner-Seitz cell.
subroutine, public wannier_calc_read_centers(wan_opts, centers)
Read wannier centers.
subroutine, public wannier_calc_wigner_seitz_count(real_lattice, mp_grid, ws_search_size, ws_distance_tol, nrpts)
Count the number of R-points in the Wigner-Seitz cell.
subroutine, public wannier_calc_write_hr_file(dir, prefix, irvec, ndegen, ham_r)
Write H(R) in the Wannier basis to a _hr.dat file.
subroutine, public wannier_calc_write_u_matrices(dir, prefix, kpoints, u_matrix, u_dis_matrix)
Write U and U_dis matrices to file.
subroutine, public wannier_calc_create_amn(wan_opts, band_index, space, mesh, latt, st, kpoints, spin_channel, num_proj, proj_input, projection)
Calculation of Wannier90 Projection Matrix.
logical function wannier_calc_r_in_wigner_seitz_cell(rvec, real_metric, mp_grid, ws_search_size, ws_distance_tol, ndegen)
Check whether a lattice point lies in the Wigner-Seitz cell of the superlattice.
subroutine, public wannier_calc_create_mmn(band_index, mesh, st, latt, spin_channel, nnlist, nncell, overlap)
Kohn-Sham State Overlap Matrix.
subroutine, public wannier_calc_read_u_matrices(wan_opts, kpoints, u_matrix, u_dis_matrix)
Read wannier transformation matrix.
subroutine wannier_calc_get_header(header_start, header)
Build a file header containing the current date and time.
subroutine, public wannier_calc_compute_ham_r(namespace, mesh, st, hm, kpoints, wannier_opts, u_matrix, irvec, ham_r)
Compute the time-dependent Hamiltonian in the Wannier basis in real space.
subroutine, public wannier_calc_write_spreads(dir, prefix, spreads)
Write wannier spreads to file.
subroutine, public wannier_calc_load_restart(namespace, mc, space, st, gr, kpoints, restart_states)
Load Octopus restart data from disk.
Wannier options module.
subroutine, public ylm_wannier(ylm, l, mr, rr, xx, nr)
Describes mesh distribution to nodes.
Definition: mesh.F90:188
The states_elec_t class contains all electronic wave functions.
Mocks the projection type from wannier90.
Wannier related options.
batches of electronic states
Definition: wfs_elec.F90:143