Octopus
wannier.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
13module wannier_oct_m
14 use, intrinsic :: iso_fortran_env
15
16 use debug_oct_m
17 use global_oct_m
19 use io_oct_m
20 use ions_oct_m
22 use mesh_oct_m
24 use mpi_oct_m
28 use parser_oct_m
33 use types_oct_m
34 use unit_oct_m
37 only: &
44
45#ifdef HAVE_WANNIER90
46 use w90_library
48#endif
49
50 implicit none
51 private
52
53 public :: wannier_t
55 type :: wannier_t
56 type(wannier_opts_t) :: options
57 complex(real64), allocatable :: u_matrix(:,:,:)
58 complex(real64), allocatable :: u_dis_matrix(:,:,:)
59 real(real64), allocatable :: centers(:,:)
60#ifdef HAVE_WANNIER90
61 type(lib_common_type) :: w90main
62#endif
63
64 contains
65 procedure :: init_from_input => wannier_init_from_input
66 procedure :: init_parallelization => wannier_init_parallelization
67 procedure :: init_data_from_file => wannier_init_data_from_file
68 procedure :: restart_write_data => wannier_restart_write_data
69 procedure :: restart_read_data => wannier_restart_read_data
70 procedure :: write_iter => wannier_write_iter
71 final :: wannier_finalize
72 end type wannier_t
73
74contains
75
79 subroutine wannier_init_from_input(this, namespace, kpoints)
80 class(wannier_t), intent(inout) :: this
81 type(namespace_t), intent(in) :: namespace
82 type(kpoints_t), intent(in) :: kpoints
83
85
86 ! Parse all Wannier related options
87 call this%options%parse_oct(namespace)
88 if (this%options%td_method /= td_wannier_method_none) then
89 call this%options%parse_win()
90 end if
91
92 safe_allocate(this%u_matrix(1:this%options%num_wann, 1:this%options%num_wann, 1:product(kpoints%nik_axis)))
93 safe_allocate(this%u_dis_matrix(1:this%options%num_bands, 1:this%options%num_wann, 1:product(kpoints%nik_axis)))
94 safe_allocate(this%centers(1:3, 1:this%options%num_wann))
95 this%u_matrix = m_zero
96 this%u_dis_matrix = m_zero
97 this%centers = m_zero
98
100 end subroutine wannier_init_from_input
101
107 subroutine wannier_init_parallelization(this, namespace, st, ions, kpoints)
108 class(wannier_t), intent(inout) :: this
109 type(namespace_t), intent(in) :: namespace
110 type(states_elec_t), intent(in) :: st
111 type(ions_t), intent(in) :: ions
112 type(kpoints_t), intent(in) :: kpoints
113
114#ifdef HAVE_WANNIER90
115 integer :: ierr
116#endif
117
119#ifdef HAVE_WANNIER90
120 if (this%options%td_method /= td_wannier_method_none) then
121 call wannier90lib_init_w90main(namespace, ions, kpoints, st, &
122 this%options, this%w90main, stdout, stderr, ierr)
123 if (ierr /= 0) then
124 write(message(1),'(a,i0)') 'Error initializing wannier90 library object: ', ierr
125 call messages_fatal(1)
126 end if
127 end if
128#endif
129
131
132 end subroutine wannier_init_parallelization
133
138 subroutine wannier_init_data_from_file(this, kpoints, space)
139 class(wannier_t), intent(inout) :: this
140 type(kpoints_t), intent(in) :: kpoints
141 type(electron_space_t), intent(in) :: space
142
144
145 if (this%options%td_method /= td_wannier_method_none) then
146 if (space%dim /= 3) then ! see issue #1466
147 call messages_not_implemented("TD wannier: only 3D is supported")
148 end if
149 call wannier_calc_read_centers(this%options, this%centers)
150 call wannier_calc_read_u_matrices(this%options, kpoints, this%u_matrix, this%u_dis_matrix)
151 end if
152#ifdef HAVE_WANNIER90
153 call w90_set_u_matrix(this%w90main, this%u_matrix)
154 call w90_set_u_opt(this%w90main, this%u_dis_matrix)
155 this%w90main%wannier_data%centres = this%centers
156#endif
157
159 end subroutine wannier_init_data_from_file
165 subroutine wannier_restart_write_data(this, namespace, mc, gr)
166 class(wannier_t), intent(inout) :: this
167 type(namespace_t), intent(in) :: namespace
168 type(multicomm_t), intent(in) :: mc
169 class(mesh_t), intent(in) :: gr
170
171 type(restart_t) :: restart
172 integer :: ierr
173
175
176 call restart%init(namespace, restart_td, restart_type_dump, mc, ierr, mesh=gr)
177 if (ierr /= 0) then
178 message(1) = 'Unable to initialize TD restart for Wannier write.'
179 call messages_fatal(1)
180 end if
181
182 call restart%write_binary('wannier_u_matrix', size(this%u_matrix), this%u_matrix, ierr)
183 if (ierr /= 0) then
184 message(1) = 'Unable to write Wannier restart data (u_matrix).'
185 call messages_fatal(1)
186 end if
187
188 call restart%write_binary('wannier_u_dis_matrix', size(this%u_dis_matrix), this%u_dis_matrix, ierr)
189 if (ierr /= 0) then
190 message(1) = 'Unable to write Wannier restart data (u_dis_matrix).'
191 call messages_fatal(1)
192 end if
193
194 call restart%write_binary('wannier_centers', size(this%centers), this%centers, ierr)
195 if (ierr /= 0) then
196 message(1) = 'Unable to write Wannier restart data (centers).'
197 call messages_fatal(1)
198 end if
199
200 call restart%end()
201
203 end subroutine wannier_restart_write_data
204
209 subroutine wannier_restart_read_data(this, namespace, mc, gr)
210 class(wannier_t), intent(inout) :: this
211 type(namespace_t), intent(in) :: namespace
212 type(multicomm_t), intent(in) :: mc
213 class(mesh_t), intent(in) :: gr
214
215 type(restart_t) :: restart
216 integer :: ierr
217
219
220 call restart%init(namespace, restart_td, restart_type_load, mc, ierr, mesh=gr)
221 if (ierr /= 0) then
222 message(1) = 'Unable to initialize TD restart for Wannier read.'
224 end if
225
226 call restart%read_binary('wannier_u_matrix', size(this%u_matrix), this%u_matrix, ierr)
227 if (ierr /= 0) then
228 message(1) = 'Unable to read Wannier restart data (u_matrix).'
229 call messages_fatal(1)
230 end if
231
232 call restart%read_binary('wannier_u_dis_matrix', size(this%u_dis_matrix), this%u_dis_matrix, ierr)
233 if (ierr /= 0) then
234 message(1) = 'Unable to read Wannier restart data (u_dis_matrix).'
235 call messages_fatal(1)
236 end if
237
238 call restart%read_binary('wannier_centers', size(this%centers), this%centers, ierr)
239 if (ierr /= 0) then
240 message(1) = 'Unable to read Wannier restart data (centers).'
241 call messages_fatal(1)
242 end if
243
244#ifdef HAVE_WANNIER90
245 call w90_set_u_matrix(this%w90main, this%u_matrix)
246 call w90_set_u_opt(this%w90main, this%u_dis_matrix)
247 this%w90main%wannier_data%centres = this%centers
248#endif
249
250 call restart%end()
251
253 end subroutine wannier_restart_read_data
254
256 subroutine wannier_write_iter(this, namespace, outp, iter, ions, kpoints)
257 class(wannier_t), intent(in) :: this
258 type(namespace_t), intent(in) :: namespace
259 type(output_t), intent(inout) :: outp
260 integer, intent(in) :: iter
261 class(ions_t), intent(in) :: ions
262 type(kpoints_t), intent(in) :: kpoints
263
264 character(len=MAX_PATH_LEN) :: dir, raw_dir
265
266 push_sub(wannier_write_iter)
267
268 ! Only write wannier90 output every n iterations, as specified in input
269 if (mod(iter, this%options%td_output_interval) == 0) then
270 write(dir, '(a,a,i7.7)') trim(outp%iter_dir),"td.", iter ! name of directory
271 raw_dir = io_workpath(trim(dir), namespace)
272
273 call io_mkdir(trim(dir), namespace, parents=.true.)
274
275 if (this%options%td_method /= td_wannier_method_none) then
276 call wannier_calc_write_u_matrices(trim(raw_dir), trim(this%options%prefix), kpoints, this%u_matrix, this%u_dis_matrix)
277 call wannier_calc_write_centers(trim(raw_dir), trim(this%options%prefix), this%centers, ions)
278 end if
279 end if
280
281 pop_sub(wannier_write_iter)
282 end subroutine wannier_write_iter
283
285 subroutine wannier_finalize(wan)
286 type(wannier_t), intent(inout) :: wan
287
288 push_sub(wannier_finalize)
289
290 safe_deallocate_a(wan%u_matrix)
291 safe_deallocate_a(wan%u_dis_matrix)
292 safe_deallocate_a(wan%centers)
293#ifdef HAVE_WANNIER90
294 ! Attributes of `lib_common_type` are just pointers to memory we own, so no additional finalisation required
295#endif
296
297 pop_sub(wannier_finalize)
298 end subroutine wannier_finalize
299
300end module wannier_oct_m
real(real64), parameter, public m_zero
Definition: global.F90:200
Definition: io.F90:116
character(len=max_path_len) function, public io_workpath(path, namespace)
construct path name from given name and namespace
Definition: io.F90:318
subroutine, public io_mkdir(fname, namespace, parents)
Definition: io.F90:361
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
This module handles the communicators for the various parallelization strategies.
Definition: multicomm.F90:147
this module contains the low-level part of the output system
Definition: output_low.F90:117
integer, parameter, public restart_type_dump
Definition: restart.F90:184
integer, parameter, public restart_td
Definition: restart.F90:156
integer, parameter, public restart_type_load
Definition: restart.F90:184
This module handles reading and writing restart information for the states_elec_t.
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.
Interface module to Wannier 90 library.
subroutine, public wannier90lib_init_w90main(namespace, ions, kpoints, st, inp_options, w90main, stdout, stderr, ierr)
Initialize wannier90 library data.
Wannier90 related calculations.
subroutine, public wannier_calc_write_centers(dir, prefix, centers, ions)
Write wannier centers to file.
subroutine, public wannier_calc_read_centers(wan_opts, centers)
Read wannier centers.
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_read_u_matrices(wan_opts, kpoints, u_matrix, u_dis_matrix)
Read wannier transformation matrix.
Wannier module.
Definition: wannier.F90:108
subroutine wannier_finalize(wan)
Clean up wannier object data.
Definition: wannier.F90:371
subroutine wannier_restart_write_data(this, namespace, mc, gr)
Write TD Wannier restart data.
Definition: wannier.F90:251
subroutine wannier_write_iter(this, namespace, outp, iter, ions, kpoints)
Write TD Wannier data every n time steps.
Definition: wannier.F90:342
subroutine wannier_init_data_from_file(this, kpoints, space)
Initialize wannier object data for TD run.
Definition: wannier.F90:224
subroutine wannier_restart_read_data(this, namespace, mc, gr)
Read TD Wannier restart data.
Definition: wannier.F90:295
subroutine wannier_init_from_input(this, namespace, kpoints)
Initialize wannier object options from input files.
Definition: wannier.F90:175
subroutine wannier_init_parallelization(this, namespace, st, ions, kpoints)
Initialize parallelization for wannier object.
Definition: wannier.F90:203
Wannier options module.
integer, parameter, public td_wannier_method_none
Describes mesh distribution to nodes.
Definition: mesh.F90:187
Stores all communicators and groups.
Definition: multicomm.F90:208
output handler class
Definition: output_low.F90:166
Main object containing all Wannier-related data and methods.
Definition: wannier.F90:150
int true(void)