Octopus
wannier90lib_interface.F90
Go to the documentation of this file.
1 !! Copyright (C) A Buccheri, J Reimann. 2026
2!!
3!! This Source Code Form is subject to the terms of the Mozilla Public
4!! License, v. 2.0. If a copy of the MPL was not distributed with this
5!! file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
7#include "global.h"
8
13
15 use debug_oct_m
16 use global_oct_m
17 use ions_oct_m, only: ions_t
18 use io_oct_m, only: io_open, io_close
19 use kpoints_oct_m, only: kpoints_t
21 use mesh_oct_m, only: mesh_t
25 use space_oct_m, only: space_t
30 only: &
35
36 ! External library
37 ! Wannier90 lib type and routines are not dressed with the preprocessor guard as they
38 ! are limited to this scope, and this module should only get included in compilation
39 ! if Wannier90 lib is linked to
40 use w90_library, only: lib_common_type, &
41 w90_set_option, &
42 w90_input_reader, &
43 w90_input_setopt, &
44 w90_set_comm
45
46 implicit none
47 private
48 public :: wannier90lib_init_w90main, &
51
52contains
53
59 subroutine wannier90lib_init_w90main(namespace, ions, kpoints, st, &
60 inp_options, w90main, stdout, stderr, ierr)
61 type(namespace_t), intent(in) :: namespace
62 type(ions_t), intent(in) :: ions
63 type(kpoints_t), intent(in) :: kpoints
64 type(states_elec_t), intent(in) :: st
65 type(wannier_opts_t), intent(in) :: inp_options
66 type(lib_common_type), intent(inout) :: w90main
67 integer, intent(inout) :: stdout, stderr, ierr
68
69#ifdef HAVE_MPI
70 integer, allocatable :: distk(:)
71 integer :: ik, iq, nk
72#endif
73
75
76 ! Set required options
77 ! Note: num_bands does not include excluded bands!
78 ! The order is nst > num_bands > num_wann
79 ! TODO: Correctly handle this depending on inputs
80 ! TODO: Distinguish everywhere between nst and num_bands
81 call w90_set_option(w90main, 'kpoints', -kpoints%reduced%red_point(1:3,:)) ! minus sign for wrong octopus convention
82 call w90_set_option(w90main, 'mp_grid', kpoints%nik_axis(1:3))
83 call w90_set_option(w90main, 'num_bands', inp_options%num_bands)
84 call w90_set_option(w90main, 'num_wann', inp_options%num_wann)
85 call w90_set_option(w90main, 'unit_cell_cart', units_from_atomic(unit_angstrom, ions%latt%rlattice(1:3,:)))
86
87#ifdef HAVE_MPI
88 nk = product(kpoints%nik_axis(1:3))
89 safe_allocate(distk(1:nk))
90 if (st%d%kpt%parallel) then
91 ! pass octopus communicator to wannier90 library
92 call w90_set_comm(w90main, st%d%kpt%mpi_grp%comm%MPI_VAL)
93 ! Feed kpoint distribution to wannier90 library
94 distk = -1
95 ! (k, spin)
96 do iq = 1, st%nik
97 ! k-only
98 ik = st%d%get_kpoint_index(iq)
99 if (distk(ik) == -1) then
100 distk(ik) = st%d%kpt%node(iq)
101 else if (distk(ik) /= st%d%kpt%node(iq)) then
102 message(1) = 'Error: Wannier90 k-point parallelization: same k-point' // &
103 'assigned to different nodes.'
104 call messages_fatal(1, namespace=namespace)
105 end if
106 end do
107 else
108 ! octopus default communicator is not acceptable in that case
109 call w90_set_comm(w90main, st%system_grp%comm%MPI_VAL)
110 ! No parallelization, assign all k-points to node 0
111 distk = 0
112 endif
113 call w90_set_option(w90main, 'distk', distk)
114 safe_deallocate_a(distk)
115#endif
116
117 ! Set atom data for projection parsing
118 call wannier90lib_set_atom_data(w90main, ions)
119
120 ! Parse projections from input file
121 call wannier90lib_set_projections(w90main, inp_options)
122
123 ! Option to dump files
124 if (inp_options%dump_inputs) then
125 call w90_set_option(w90main, "dump_inputs", .true.)
126 end if
127
128 ! Initialize library type with seedname
129 call w90_input_setopt(w90main, inp_options%prefix, stdout, stderr, ierr)
130
131 ! Read optional options from file
132 ! Note: Mandatory options are only read from Octopus's input.
133 ! If any mandatory options are set in `.win`, they will be ignored triggering a warning.
134 call wannier90lib_warn_inputs(inp_options)
135 call w90_input_reader(w90main, stdout, stderr, ierr)
136 if (ierr /= 0) then
137 write(message(1), '(a,i0)') 'Error in w90_input_reader: ', ierr
138 call messages_fatal(1)
139 end if
140
142 end subroutine wannier90lib_init_w90main
143
144
150 subroutine wannier90lib_set_atom_data(w90main, ions)
151 type(lib_common_type), intent(inout) :: w90main
152 type(ions_t), intent(in) :: ions
153
154 integer :: ia, n_atoms
155 real(real64), allocatable :: atom_pos_frac(:,:)
156 character(len=2), allocatable :: atom_labels(:)
157
159
160 n_atoms = ions%natoms
161
162 ! Allocate arrays for atom data
163 safe_allocate(atom_pos_frac(1:3, 1:n_atoms))
164 safe_allocate(atom_labels(1:n_atoms))
165
166 ! Extract atomic positions and labels from ions
167 do ia = 1, n_atoms
168 ! Get Cartesian positions from octopus
169 ! Convert to fractional (reduced) coordinates for wannier90
170 atom_pos_frac(1:3, ia) = ions%latt%cart_to_red(ions%pos(1:3, ia))
171 ! Get atomic labels
172 atom_labels(ia) = trim(ions%atom(ia)%label)
173 end do
174
175 ! Set atom positions in fractional coordinates
176 call w90_set_option(w90main, 'atoms_frac', atom_pos_frac)
177 ! Set symbols
178 call w90_set_option(w90main, 'symbols', atom_labels)
179
180 safe_deallocate_a(atom_pos_frac)
181 safe_deallocate_a(atom_labels)
182
184 end subroutine wannier90lib_set_atom_data
185
186
191 subroutine wannier90lib_set_projections(w90main, inp_options)
192 type(lib_common_type), intent(inout) :: w90main
193 type(wannier_opts_t), intent(in) :: inp_options
194
195 character(len=80) :: filename, line
196 integer :: w90_win, io
197 logical :: exist
198
200
201 ! Correctly handle projections by parsing them from .win file
202 filename = trim(adjustl(inp_options%prefix)) //'.win'
203
204 message(1) = "oct-wannier90: Parsing projections from "//filename
205 call messages_info(1)
206
207 inquire(file=filename,exist=exist)
208 if (.not. exist) then
209 message(1) = 'oct-wannier90: Cannot find specified Wannier90 win file.'
210 call messages_fatal(1)
211 end if
212
213 ! read projections and set them in w90main
214 w90_win = io_open(trim(filename), action='read')
215 do
216 read(w90_win, '(A)', iostat=io) line
217 if (is_iostat_end(io)) exit
218
219 if (trim(line) == 'begin projections') then
220 do
221 read(w90_win, '(A)') line
222 if (trim(line) == 'end projections') exit
223 call w90_set_option(w90main, 'projections', trim(line))
224 end do
225 end if
226 end do
227 call io_close(w90_win)
228
230 end subroutine wannier90lib_set_projections
231
237 subroutine wannier90lib_warn_inputs(inp_options)
238 type(wannier_opts_t), intent(in) :: inp_options
239
240 character(len=80) :: filename, line, dummy, dummy1
241 integer :: w90_win, io
242 logical :: exist
243
245
246 filename = trim(adjustl(inp_options%prefix)) //'.win'
247
248 message(1) = "oct-wannier90: Parsing "//filename
249 call messages_info(1)
250
251 inquire(file=filename,exist=exist)
252 if (.not. exist) then
253 message(1) = 'oct-wannier90: Cannot find specified Wannier90 win file.'
254 call messages_fatal(1)
255 end if
257 ! warn which options are not considered from .win file
258 w90_win = io_open(trim(filename), action='read')
259 do
260 read(w90_win, '(A)', iostat=io) line
261 if (is_iostat_end(io)) exit
262
263 if (trim(line) == 'begin kpoints') then
264 message(1) = 'oct-wannier90: Warning: kpoints specified in .win file' // &
265 ' are ignored. Using kpoints from octopus input instead.'
266 call messages_warning(1)
267 else if (trim(line) == 'begin atoms_frac') then
268 message(1) = 'oct-wannier90: Warning: atomic positions specified in .win file' // &
269 ' are ignored. Using positions from octopus input instead.'
270 call messages_warning(1)
271 else if (trim(line) == 'begin unit_cell_cart') then
272 message(1) = 'oct-wannier90: Warning: unit cell vectors specified in .win file' // &
273 ' are ignored. Using unit cell vectors from octopus input instead.'
274 call messages_warning(1)
275 end if
276 if (index(line, '=') <= 0) then
277 read(line, *, iostat=io) dummy, dummy1
278 if(dummy == 'mp_grid') then
279 message(1) = 'oct-wannier90: Warning: mp_grid specified in .win file' // &
280 ' is ignored. Using k-point mesh from octopus input instead.'
281 call messages_warning(1)
282 end if
283 end if
284 end do
285 call io_close(w90_win)
286
288 end subroutine wannier90lib_warn_inputs
289
293 subroutine wannier90lib_create_wannier90_amn(w90main, inp_options, exclude_list, &
294 band_index, space, mesh, latt, st, kpoints, projection)
295 type(lib_common_type), intent(in) :: w90main
296 type(wannier_opts_t), intent(in) :: inp_options
297 class(space_t), intent(in) :: space
298 class(mesh_t), intent(in) :: mesh
299 type(lattice_vectors_t), intent(in) :: latt
300 type(states_elec_t), intent(in) :: st
301 type(kpoints_t), intent(in) :: kpoints
302 logical, intent(in) :: exclude_list(:)
303 integer, intent(in) :: band_index(:)
304
305 complex(real64), contiguous, intent(out) :: projection(:, :, :)
306 type(wannier_calc_proj_t), allocatable :: proj_input_oct(:)
307
308 integer :: iw
309
311 call profiling_in("W90_AMN")
312
313 ! Note: in w90main, proj_input contains the projections from input file,
314 ! proj contains the projections that are actually used in wannier90
315 ! We use proj_input since that is also what is written to file in -pp mode
316
317
318 safe_allocate(proj_input_oct(1:w90main%num_proj))
319 do iw = 1, w90main%num_proj
320 proj_input_oct(iw)%l = w90main%proj_input(iw)%l
321 proj_input_oct(iw)%m = w90main%proj_input(iw)%m
322 proj_input_oct(iw)%s = w90main%proj_input(iw)%s
323 proj_input_oct(iw)%radial = w90main%proj_input(iw)%radial
324 proj_input_oct(iw)%site = w90main%proj_input(iw)%site
325 proj_input_oct(iw)%s_qaxis = w90main%proj_input(iw)%s_qaxis
326 proj_input_oct(iw)%z = w90main%proj_input(iw)%z
327 proj_input_oct(iw)%x = w90main%proj_input(iw)%x
328 proj_input_oct(iw)%zona = w90main%proj_input(iw)%zona
329 end do
330
331 call wannier_calc_create_amn(inp_options, exclude_list, &
332 band_index, space, mesh, latt, st, kpoints, &
333 w90main%wvfn_read%spin_channel, w90main%num_proj, &
334 proj_input_oct, projection)
335
336 safe_deallocate_a(proj_input_oct)
337
338 call profiling_out("W90_AMN")
339
341
343
347 subroutine wannier90lib_create_wannier90_mmn(w90main, exclude_list, band_index, mesh, st, ions, overlap)
348 type(lib_common_type), intent(in) :: w90main
349 logical, intent(in) :: exclude_list(:)
350 integer, intent(in) :: band_index(:)
351 class(mesh_t), intent(in) :: mesh
352 type(states_elec_t), target, intent(in) :: st
353 type(ions_t), intent(in) :: ions
354
355 complex(real64), contiguous, intent(out) :: overlap(:, :, :, :)
356
359 call wannier_calc_create_mmn(exclude_list, band_index, &
360 mesh, st, ions, w90main%wvfn_read%spin_channel, &
361 w90main%kmesh_info%nnlist, w90main%kmesh_info%nncell, &
362 overlap)
363
365
367
368
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
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:120
subroutine, public messages_warning(no_lines, all_nodes, namespace)
Definition: messages.F90:525
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:162
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:410
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:594
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
Definition: profiling.F90:631
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
Definition: profiling.F90:554
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.
Interface module to Wannier 90 library.
subroutine, public wannier90lib_create_wannier90_amn(w90main, inp_options, exclude_list, band_index, space, mesh, latt, st, kpoints, projection)
Calculate wannier90 Projection Matrix.
subroutine wannier90lib_set_atom_data(w90main, ions)
Set atom data in wannier90 library.
subroutine, public wannier90lib_create_wannier90_mmn(w90main, exclude_list, band_index, mesh, st, ions, overlap)
Kohn-Sham State Overlap Matrix.
subroutine, public wannier90lib_init_w90main(namespace, ions, kpoints, st, inp_options, w90main, stdout, stderr, ierr)
Initialize wannier90 library data.
subroutine wannier90lib_set_projections(w90main, inp_options)
Parse projections.
subroutine wannier90lib_warn_inputs(inp_options)
Check for options in .win file.
Wannier90 related calculations.
subroutine, public wannier_calc_create_amn(wan_opts, exclude_list, band_index, space, mesh, latt, st, kpoints, spin_channel, num_proj, proj_input, projection)
Calculation of Wannier90 Projection Matrix.
subroutine, public wannier_calc_create_mmn(exclude_list, band_index, mesh, st, ions, spin_channel, nnlist, nncell, overlap)
Kohn-Sham State Overlap Matrix.
Wannier options module.
Describes mesh distribution to nodes.
Definition: mesh.F90:187
The states_elec_t class contains all electronic wave functions.
Mocks the projection type from wannier90.
Wannier related options.
int true(void)