Octopus
states_elec_restart.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2016 M. Marques, A. Castro, A. Rubio, G. Bertsch, X. Andrade
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
24#ifdef HAVE_ADIOS2
25 use adios2
26#endif
27 use batch_oct_m
29 use comm_oct_m
30 use debug_oct_m
32 use global_oct_m
34 use, intrinsic :: iso_fortran_env
38 use loct_oct_m
39 use math_oct_m
40 use mesh_oct_m
44 use mpi_oct_m
48 use parser_oct_m
52 use smear_oct_m
53 use space_oct_m
58 use string_oct_m
59 use types_oct_m
61
62 implicit none
63
64
65 private
66 public :: &
78
79contains
80
81 ! ---------------------------------------------------------
82 subroutine states_elec_look_and_load(restart, namespace, space, st, mesh, kpoints, fixed_occ, is_complex, packed)
83 type(restart_t), intent(in) :: restart
84 type(namespace_t), intent(in) :: namespace
85 class(space_t), intent(in) :: space
86 type(states_elec_t), target, intent(inout) :: st
87 class(mesh_t), intent(in) :: mesh
88 type(kpoints_t), intent(in) :: kpoints
89 logical, intent(in) :: fixed_occ
90 logical, optional, intent(in) :: is_complex
91 logical, optional, intent(in) :: packed
92
93 integer :: nkpt, dim, nst, ierr
94 real(real64), allocatable :: new_occ(:,:)
97
98 !check how many wfs we have
99 call states_elec_look(restart, nkpt, dim, nst, ierr)
100 if (ierr /= 0) then
101 message(1) = "Unable to read states information."
102 call messages_fatal(1, namespace=namespace)
103 end if
104
105 if (st%parallel_in_states) then
106 message(1) = "Internal error: cannot use states_elec_look_and_load when parallel in states."
107 call messages_fatal(1, namespace=namespace)
108 end if
109
110 ! Resize st%occ, retaining information there
111 allocate(new_occ(1:nst, 1:st%nik))
112 new_occ(:,:) = m_zero
113 new_occ(1:min(nst, st%nst),:) = st%occ(1:min(nst, st%nst),:)
114 safe_deallocate_a(st%occ)
115 call move_alloc(new_occ, st%occ)
116
117 ! FIXME: This wrong, one cannot just change the number of states
118 ! without updating the internal structures, in the case of parallel in states.
119 ! I think it is right now without state parallelism.
120 st%nst = nst
121 st%st_start = 1
122 st%st_end = nst
123 st%lnst = nst
124
125 safe_deallocate_a(st%node)
126 safe_allocate(st%node(1:st%nst))
127 st%node(:) = 0
128
129 safe_deallocate_a(st%eigenval)
130 safe_allocate(st%eigenval(1:st%nst, 1:st%nik))
131 st%eigenval = huge(st%eigenval)
132
133 if (present(is_complex)) then
134 if (is_complex) then
135 call states_elec_allocate_wfns(st, mesh, type_cmplx, packed=optional_default(packed, .false.))
136 else
137 call states_elec_allocate_wfns(st, mesh, type_float, packed=optional_default(packed, .false.))
138 end if
139 else
140 ! allow states_elec_allocate_wfns to decide for itself whether complex or real needed
141 call states_elec_allocate_wfns(st, mesh, packed=optional_default(packed, .false.))
142 end if
143
144 if (st%d%ispin == spinors) then
145 safe_allocate(st%spin(1:3, 1:st%nst, 1:st%nik))
146 st%spin = m_zero
147 end if
148
149 ! load wavefunctions
150 call states_elec_load(restart, namespace, space, st, mesh, kpoints, fixed_occ, ierr)
151 if (ierr /= 0) then
152 message(1) = "Unable to read wavefunctions."
153 call messages_fatal(1, namespace=namespace)
154 end if
155
157 end subroutine states_elec_look_and_load
158
159
160 ! ---------------------------------------------------------
161 subroutine states_elec_dump(restart, space, st, mesh, kpoints, ierr, iter, lr, verbose)
162 type(restart_t), intent(in) :: restart
163 class(space_t), intent(in) :: space
164 type(states_elec_t), target, intent(in) :: st
165 class(mesh_t), intent(in) :: mesh
166 type(kpoints_t), intent(in) :: kpoints
167 integer, intent(out) :: ierr
168 integer, optional, intent(in) :: iter
169 type(lr_t), optional, intent(in) :: lr
170 logical, optional, intent(in) :: verbose
171
172 integer :: iunit_wfns, iunit_occs, iunit_states
173 integer :: err, err2(2), ik, idir, ist, idim, itot
174 integer :: root(1:p_strategy_max)
175 character(len=MAX_PATH_LEN) :: filename
176 character(len=500) :: lines(3)
177 logical :: lr_wfns_are_associated, should_write, verbose_
178 real(real64) :: kpoint(space%dim)
179 real(real64), allocatable :: dpsi(:)
180 complex(real64), allocatable :: zpsi(:)
181 integer :: restart_file_format
182
183 push_sub(states_elec_dump)
184
185 verbose_ = optional_default(verbose, .true.)
186
187 ierr = 0
188
189 if (restart%skip()) then
190 pop_sub(states_elec_dump)
191 return
192 end if
193
194 if (verbose_) then
195 message(1) = "Info: Writing states."
196 call print_date(trim(message(1))//' ')
197 end if
198
199 call profiling_in("RESTART_WRITE")
200
201 restart_file_format = restart%file_format_states
202 if (present(lr)) then
203 lr_wfns_are_associated = (allocated(lr%ddl_psi) .and. states_are_real(st)) .or. &
204 (allocated(lr%zdl_psi) .and. states_are_complex(st))
205 assert(lr_wfns_are_associated)
206 ! always use obf format for linear response
207 restart_file_format = option__restartfileformatstates__obf
208 end if
209
211
212 iunit_states = restart%open('states')
213 write(lines(1), '(a20,1i10)') 'nst= ', st%nst
214 write(lines(2), '(a20,1i10)') 'dim= ', st%d%dim
215 write(lines(3), '(a20,1i10)') 'nik= ', st%nik
216 call restart%write(iunit_states, lines, 3, err)
217 if (err /= 0) ierr = ierr + 1
218 call restart%close(iunit_states)
219
220
221 iunit_wfns = restart%open('wfns')
222 lines(1) = '# #k-point #st #dim filename'
223 if (states_are_real(st)) then
224 lines(2) = '%Real_Wavefunctions'
225 else
226 lines(2) = '%Complex_Wavefunctions'
227 end if
228 call restart%write(iunit_wfns, lines, 2, err)
229 if (err /= 0) ierr = ierr + 2
230
231
232 iunit_occs = restart%open('occs')
233 lines(1) = '# occupations | eigenvalue[a.u.] | Im(eigenvalue) [a.u.] | k-points | k-weights | filename | ik | ist | idim'
234 lines(2) = '%Occupations_Eigenvalues_K-Points'
235 call restart%write(iunit_occs, lines, 2, err)
236 if (err /= 0) ierr = ierr + 4
237
238
239 if (states_are_real(st)) then
240 safe_allocate(dpsi(1:mesh%np))
241 else
242 safe_allocate(zpsi(1:mesh%np))
243 end if
244
245 itot = 1
246 root = -1
247 err2 = 0
248 do ik = 1, st%nik
249 kpoint(1:space%dim) = &
250 kpoints%get_point(st%d%get_kpoint_index(ik), absolute_coordinates = .true.)
251
252 do ist = 1, st%nst
253 do idim = 1, st%d%dim
254 root(p_strategy_domains) = mod(itot - 1, mesh%mpi_grp%size)
255 write(filename,'(i10.10)') itot
256
257 write(lines(1), '(i8,a,i8,a,i8,3a)') ik, ' | ', ist, ' | ', idim, ' | "', trim(filename), '"'
258 call restart%write(iunit_wfns, lines, 1, err)
259 if (err /= 0) err2(1) = err2(1) + 1
260
261 write(lines(1), '(e23.16,a,e23.16)') st%occ(ist,ik), ' | ', st%eigenval(ist, ik)
262 write(lines(1), '(a,a,e23.16)') trim(lines(1)), ' | ', m_zero
263 do idir = 1, space%dim
264 write(lines(1), '(a,a,e23.16)') trim(lines(1)), ' | ', kpoint(idir)
265 end do
266 write(lines(1), '(a,a,e23.16,a,i10.10,3(a,i8))') trim(lines(1)), &
267 ' | ', st%kweights(ik), ' | ', itot, ' | ', ik, ' | ', ist, ' | ', idim
268 call restart%write(iunit_occs, lines, 1, err)
269 if (err /= 0) err2(1) = err2(1) + 1
270
271 should_write = st%st_start <= ist .and. ist <= st%st_end
272
273 if (restart_file_format == option__restartfileformatstates__obf) then
274 if (should_write) then
275 if (st%d%kpt%start <= ik .and. ik <= st%d%kpt%end) then
276 if (states_are_real(st)) then
277 if (.not. present(lr)) then
278 call states_elec_get_state(st, mesh, idim, ist, ik, dpsi)
279 call restart%write_mesh_function(filename, mesh, dpsi, err, root = root)
280 else
281 call restart%write_mesh_function(filename, mesh, &
282 lr%ddl_psi(:, idim, ist, ik), err, root = root)
283 end if
284 else
285 if (.not. present(lr)) then
286 call states_elec_get_state(st, mesh, idim, ist, ik, zpsi)
287 call restart%write_mesh_function(filename, mesh, zpsi, err, root = root)
288 else
289 call restart%write_mesh_function(filename, mesh, &
290 lr%zdl_psi(:, idim, ist, ik), err, root = root)
291 end if
292 end if
293 else
294 err = 0
295 end if
296 end if
297 if (err /= 0) err2(2) = err2(2) + 1
298 end if
299
300 itot = itot + 1
301 end do ! st%d%dim
302 end do ! st%nst
303 end do ! st%nik
304 if (err2(1) /= 0) ierr = ierr + 8
305 if (err2(2) /= 0) ierr = ierr + 16
306
307 safe_deallocate_a(dpsi)
308 safe_deallocate_a(zpsi)
309
310 if (restart_file_format == option__restartfileformatstates__adios2) then
311 if (present(lr)) then
312 ! this should never occur because we force obf format for linear response
313 call messages_not_implemented("ADIOS2 restart file format with linear response.")
314 end if
315 call states_elec_dump_adios2(restart, st, mesh, ierr)
316 end if
317
318 lines(1) = '%'
319 call restart%write(iunit_occs, lines, 1, err)
320 if (err /= 0) ierr = ierr + 32
321 call restart%write(iunit_wfns, lines, 1, err)
322 if (err /= 0) ierr = ierr + 64
323 if (present(iter)) then
324 write(lines(1),'(a,i7)') 'Iter = ', iter
325 call restart%write(iunit_wfns, lines, 1, err)
326 if (err /= 0) ierr = ierr + 128
327 end if
328
329 call restart%close(iunit_wfns)
330 call restart%close(iunit_occs)
331
332 if (verbose_) then
333 message(1) = "Info: Finished writing states."
334 call print_date(trim(message(1))//' ')
335 end if
336
338
339 call profiling_out("RESTART_WRITE")
340 pop_sub(states_elec_dump)
341 end subroutine states_elec_dump
342
343 ! ---------------------------------------------------------
344 subroutine states_elec_dump_adios2(restart, st, mesh, ierr)
345 type(restart_t), intent(in) :: restart
346 type(states_elec_t), target, intent(in) :: st
347 class(mesh_t), intent(in) :: mesh
348 integer, intent(out) :: ierr
349
350#ifdef HAVE_ADIOS2
351 type(adios2_adios) :: adios
352 type(adios2_io) :: io
353 type(adios2_variable) :: var, var_indices
354 type(adios2_attribute) :: attribute
355 type(adios2_engine) :: engine
356 integer :: adios2_type, ik, ib, ip, adios2_mode
357 integer(int64), allocatable :: global_indices(:)
358 class(wfs_elec_t), pointer :: psib
359#endif
361
362#ifdef HAVE_ADIOS2
363#ifdef HAVE_MPI
364 call adios2_init(adios, restart%mpi_grp%comm%MPI_VAL, ierr)
365#else
366 call adios2_init(adios, ierr)
367#endif
368 call check_error(.not. adios%valid .or. ierr /= adios2_error_none, "Problem initializing ADIOS2.")
369 call adios2_declare_io(io, adios, "writer", ierr)
370 call check_error(.not. io%valid .or. ierr /= adios2_error_none, "Problem initializing ADIOS2.")
371 ! do not compute min and max
372 call adios2_set_parameter(io, "StatsLevel", "0", ierr)
373 call check_error(ierr /= adios2_error_none, "Problem setting ADIOS2 parameter.")
374 ! open restart file
375 call adios2_open(engine, io, trim(restart%dir())//"/"//"wavefunctions.bp", adios2_mode_write, ierr)
376 call check_error(.not. engine%valid .or. ierr /= adios2_error_none, "Problem opening ADIOS2 restart file.")
377 if (states_are_real(st)) then
378 adios2_type = adios2_type_dp
379 else
380 adios2_type = adios2_type_complex_dp
381 end if
382 ! define one variable for all states
383 call adios2_define_variable(var, io, "wavefunctions", adios2_type, 3, &
384 [int(st%nst * st%d%dim, int64), int(mesh%np_global, int64), int(st%nik, int64)], & ! global size of wavefunctions
385 [0_int64, 0_int64, 0_int64], & ! local selection of the variable, overwritten by set_selection call below
386 [1_int64, 1_int64, 1_int64], &
387 adios2_variable_dims, ierr)
388 call check_error(.not. var%valid .or. ierr /= adios2_error_none, "Problem creating ADIOS2 variable (wavefunctions).")
389 ! save ParDomains in an attribute
390 call adios2_define_attribute(attribute, io, "ParDomains", mesh%mpi_grp%size, ierr)
391 call check_error(.not. attribute%valid .or. ierr /= adios2_error_none, "Problem creating ADIOS2 attribute (ParDomains).")
392 ! define one variable for all states
393 call adios2_define_variable(var_indices, io, "global_indices", adios2_type_integer8, 1, &
394 [int(mesh%np_global, int64)], & ! global size of array
395 [int(mesh%pv%xlocal-1, int64)], & ! local offset
396 [int(mesh%np, int64)], & ! local size
397 adios2_variable_dims, ierr)
398 call check_error(.not. var_indices%valid .or. ierr /= adios2_error_none, "Problem creating ADIOS2 variable (global_indices).")
399 ! save grid indices
400 safe_allocate(global_indices(mesh%np))
401 do ip = 1, mesh%np
402 global_indices(ip) = par_vec_local2global(mesh%pv, ip)
403 end do
404
405 call adios2_begin_step(engine, ierr)
406 call adios2_put(engine, var_indices, global_indices, ierr)
407
408 do ik = st%d%kpt%start, st%d%kpt%end
409 do ib = st%group%block_start, st%group%block_end
410 ! make sure we have a batch in BATCH_PACKED status
411 select case (st%group%psib(ib, ik)%status())
412 case (batch_not_packed)
413 safe_allocate(psib)
414 call st%group%psib(ib, ik)%copy_to(psib, copy_data=.true.)
415 ! transform the unpacked batch to a packed batch on the CPU
416 call psib%do_pack(batch_packed)
417 ! we need to use sync mode in this case because we use a temporary batch
418 adios2_mode = adios2_mode_sync
419 case (batch_packed)
420 psib => st%group%psib(ib, ik)
421 adios2_mode = adios2_mode_deferred
423 safe_allocate(psib)
424 call st%group%psib(ib, ik)%copy_to(psib, copy_data=.true.)
425 ! copy to CPU
426 call psib%do_unpack(force=.true.)
427 ! make sure the batch is in packed state on the CPU
428 call psib%do_pack(batch_packed)
429 ! we need to use sync mode in this case because we use a temporary batch
430 adios2_mode = adios2_mode_sync
431 end select
432 assert(psib%status() == batch_packed)
433
434 ! select part of variable to write, correspond to the memory of the current batch
435 ! first argument gives the start in the global array (-1 needed for C indexing in ADIOS2)
436 ! second argument gives the size of the local batch
437 call adios2_set_selection(var, 3, &
438 [int((states_elec_block_min(st, ib)-1)*st%d%dim, int64), int(mesh%pv%xlocal-1, int64), int(ik-1, int64)], &
439 [int(st%group%psib(ib, ik)%nst_linear, int64), int(mesh%np, int64), 1_int64], ierr)
440 ! set memory layout of one batch, take into account padding and ghost/boundary points
441 if (states_are_real(st)) then
442 call adios2_set_memory_selection(var, 3, [0_int64, 0_int64, 0_int64], &
443 [int(size(psib%dff_pack, 1), int64), &
444 int(size(psib%dff_pack, 2), int64), 1_int64], ierr)
445 call adios2_put(engine, var, psib%dff_pack, adios2_mode, ierr)
446 else
447 call adios2_set_memory_selection(var, 3, [0_int64, 0_int64, 0_int64], &
448 [int(size(psib%zff_pack, 1), int64), &
449 int(size(psib%zff_pack, 2), int64), 1_int64], ierr)
450 call adios2_put(engine, var, psib%zff_pack, adios2_mode, ierr)
451 end if
452 select case (st%group%psib(ib, ik)%status())
454 call psib%end()
455 safe_deallocate_p(psib)
456 case (batch_packed)
457 nullify(psib)
458 end select
459 end do
460 end do
461 call adios2_end_step(engine, ierr)
462
463 ! deallocate only after end_step because only now everything has been written
464 safe_deallocate_a(global_indices)
465
466 ! close restart file
467 call adios2_close(engine, ierr)
468 call check_error(ierr /= adios2_error_none, "Problem closing ADIOS2 engine.")
469 call adios2_finalize(adios, ierr)
470 call check_error(ierr /= adios2_error_none, "Problem finalizing ADIOS2.")
471#else
472 ! avoid empty routine
473 ierr = 0
474#endif
475
477 end subroutine states_elec_dump_adios2
478
479
480 ! ---------------------------------------------------------
485 subroutine states_elec_load(restart, namespace, space, st, mesh, kpoints, fixed_occ, &
486 ierr, iter, lr, lowest_missing, label, verbose, skip)
487 type(restart_t), intent(in) :: restart
488 type(namespace_t), intent(in) :: namespace
489 class(space_t), intent(in) :: space
490 type(states_elec_t), target, intent(inout) :: st
491 class(mesh_t), intent(in) :: mesh
492 type(kpoints_t), intent(in) :: kpoints
493 logical, intent(in) :: fixed_occ
494 integer, intent(out) :: ierr
495 integer, optional, intent(out) :: iter
496 type(lr_t), optional, intent(inout) :: lr
497 integer, contiguous, optional, intent(out) :: lowest_missing(:, :)
498 character(len=*), optional, intent(in) :: label
499 logical, optional, intent(in) :: verbose
500 logical, contiguous, optional, intent(in) :: skip(:)
501
502 integer :: states_elec_file, wfns_file, occ_file, err, ik, ist, idir, idim
503 integer :: idone, iread, ntodo
504 character(len=12) :: filename
505 character(len=1) :: char
506 logical, allocatable :: filled(:, :, :)
507 character(len=256) :: lines(3), label_
508 character(len=50) :: str
509
510 real(real64) :: my_occ, imev, my_kweight
511 logical :: read_occ, lr_allocated, verbose_
512 logical :: integral_occs
513 real(real64), allocatable :: dpsi(:)
514 complex(real64), allocatable :: zpsi(:)
515 character(len=256), allocatable :: restart_file(:, :, :)
516 logical, allocatable :: restart_file_present(:, :, :)
517 real(real64) :: kpoint(space%dim), read_kpoint(space%dim)
518
519 integer :: iread_tmp
520 integer, allocatable :: lowest_missing_tmp(:, :)
521 integer :: restart_file_format
522
523 push_sub(states_elec_load)
524
525 ierr = 0
526
527 ! make sure these intent(out)`s are initialized no matter what
528 if (present(lowest_missing)) lowest_missing = 1
529 if (present(iter)) iter = 0
530
531 if (present(skip)) then
532 assert(ubound(skip, dim = 1) == st%nst)
533 end if
534
535 if (restart%skip()) then
536 ierr = -1
537 pop_sub(states_elec_load)
538 return
539 end if
540
541 call profiling_in("RESTART_READ")
542
543 verbose_ = optional_default(verbose, .true.)
544
545 if (present(label)) then
546 label_ = trim(label)
547 else
548 if (present(lr)) then
549 label_ = " for linear response"
550 else
551 label_ = ""
552 end if
553 end if
554
555 message(1) = 'Info: Reading states'
556 if (len(trim(label_)) > 0) then
557 message(1) = trim(message(1)) // trim(label_)
558 end if
559 message(1) = trim(message(1)) // "."
560 if (verbose_) call print_date(trim(message(1))//' ')
561
562 if (.not. present(lr)) then
563 st%fromScratch = .false. ! obviously, we are using restart info
564 end if
565
566 ! If one restarts a GS calculation changing the %Occupations block, one
567 ! cannot read the occupations, otherwise these overwrite the ones from
568 ! the input file. restart_fixed_occ makes that we do use the ones in the file.
569 integral_occs = .true. ! only used if restart_fixed_occ
570 if (fixed_occ) then
571 read_occ = .true.
572 st%fixed_occ = .true.
573 else
574 read_occ = .not. st%fixed_occ
575 end if
576
577 if (.not. present(lr)) then
578 st%eigenval(:, :) = m_zero
579 ! to be filled in from reading afterward
580 end if
581
582 if (.not. present(lr) .and. read_occ) then
583 st%occ(:, :) = m_zero
584 ! to be filled in from reading afterward
585 end if
586
587 restart_file_format = restart%file_format_states
588 ! sanity check
589 if (present(lr)) then
590 lr_allocated = (allocated(lr%ddl_psi) .and. states_are_real(st)) .or. &
591 (allocated(lr%zdl_psi) .and. states_are_complex(st))
592 assert(lr_allocated)
593 ! always use obf format for linear response
594 restart_file_format = option__restartfileformatstates__obf
595 end if
596
597 states_elec_file = restart%open('states')
598 ! sanity check on spin/k-points. Example file 'states':
599 ! nst= 2
600 ! dim= 1
601 ! nik= 2
602 call restart%read(states_elec_file, lines, 3, err)
603 if (err /= 0) then
604 ierr = ierr - 2
605 else
606 read(lines(2), *) str, idim
607 read(lines(3), *) str, ik
608 if (idim == 2 .and. st%d%dim == 1) then
609 write(message(1),'(a)') 'Incompatible restart information: saved calculation is spinors, this one is not.'
610 call messages_warning(1, namespace=namespace)
611 ierr = ierr - 2**2
612 end if
613 if (idim == 1 .and. st%d%dim == 2) then
614 write(message(1),'(a)') 'Incompatible restart information: this calculation is spinors, saved one is not.'
615 call messages_warning(1, namespace=namespace)
616 ierr = ierr - 2**3
617 end if
618 if (ik < st%nik) then
619 write(message(1),'(a)') 'Incompatible restart information: not enough k-points.'
620 write(message(2),'(2(a,i6))') 'Expected ', st%nik, ' > Read ', ik
621 call messages_warning(2, namespace=namespace)
622 end if
623 ! We will check that they are the right k-points later, so we do not need to put a specific error here.
624 end if
625 call restart%close(states_elec_file)
626
627
628 ! open files to read
629 wfns_file = restart%open('wfns')
630 occ_file = restart%open('occs')
631 call restart%read(wfns_file, lines, 2, err)
632 if (err /= 0) then
633 ierr = ierr - 2**5
634 else if (states_are_real(st)) then
635 read(lines(2), '(a)') str
636 if (str(2:8) == 'Complex') then
637 message(1) = "Cannot read real states from complex wavefunctions."
638 call messages_warning(1, namespace=namespace)
639 ierr = ierr - 2**6
640 else if (str(2:5) /= 'Real') then
641 message(1) = "Restart file 'wfns' does not specify real/complex; cannot check compatibility."
642 call messages_warning(1, namespace=namespace)
643 end if
644 end if
645 ! complex can be restarted from real, so there is no problem.
646
647 ! Skip two lines.
648 call restart%read(occ_file, lines, 2, err)
649 if (err /= 0) ierr = ierr - 2**7
650
651 ! If any error occurred up to this point then it is not worth continuing,
652 ! as there something fundamentally wrong with the restart files
653 if (ierr /= 0) then
654 call restart%close(wfns_file)
655 call restart%close(occ_file)
656 call profiling_out("RESTART_READ")
657 pop_sub(states_elec_load)
658 return
659 end if
660
661 if (states_are_real(st)) then
662 safe_allocate(dpsi(1:mesh%np))
663 else
664 safe_allocate(zpsi(1:mesh%np))
665 end if
666
667 safe_allocate(restart_file(1:st%d%dim, st%st_start:st%st_end, 1:st%nik))
668 safe_allocate(restart_file_present(1:st%d%dim, st%st_start:st%st_end, 1:st%nik))
669 restart_file_present = .false.
670
671 ! Next we read the list of states from the files.
672 ! Errors in reading the information of a specific state from the files are ignored
673 ! at this point, because later we will skip reading the wavefunction of that state.
674 do
675 call restart%read(wfns_file, lines, 1, err)
676 if (err == 0) then
677 read(lines(1), '(a)') char
678 if (char == '%') then
679 !We reached the end of the file
680 exit
681 else
682 read(lines(1), *) ik, char, ist, char, idim, char, filename
683 end if
684 end if
685
686 if (err /= 0 .or. index_is_wrong()) then
687 call restart%read(occ_file, lines, 1, err)
688 cycle
689 end if
690
691 if (ist >= st%st_start .and. ist <= st%st_end .and. &
692 ik >= st%d%kpt%start .and. ik <= st%d%kpt%end) then
693
694 restart_file(idim, ist, ik) = trim(filename)
695 restart_file_present(idim, ist, ik) = .true.
696 end if
697
698 call restart%read(occ_file, lines, 1, err)
699 if (.not. present(lr)) then ! do not read eigenvalues or occupations when reading linear response
700 ! # occupations | eigenvalue[a.u.] | Im(eigenvalue) [a.u.] | k-points | k-weights | filename | ik | ist | idim
701
702 if (err == 0) then
703 read(lines(1), *) my_occ, char, st%eigenval(ist, ik), char, imev, char, &
704 (read_kpoint(idir), char, idir = 1, space%dim), my_kweight
705 ! we do not want to read the k-weights, we have already set them appropriately
706 else
707 ! There is a problem with this states information, so we skip it.
708 if (ist >= st%st_start .and. ist <= st%st_end .and. &
709 ik >= st%d%kpt%start .and. ik <= st%d%kpt%end) then
710 restart_file_present(idim, ist, ik) = .false.
711 end if
712 cycle
713 end if
714
715 kpoint(1:space%dim) = &
716 kpoints%get_point(st%d%get_kpoint_index(ik), absolute_coordinates = .true.)
717 ! FIXME: maybe should ignore ik and just try to match actual vector k-points?
718 if (any(abs(kpoint(1:space%dim) - read_kpoint(1:space%dim)) > 1e-12_real64)) then
719 ! write only once for each k-point so as not to be too verbose
720 if (ist == 1) then
721 write(message(1),'(a,i6)') 'Incompatible restart information: k-point mismatch for ik ', ik
722 write(message(2),'(a,99f18.12)') ' Expected : ', kpoint(1:space%dim)
723 write(message(3),'(a,99f18.12)') ' Read : ', read_kpoint(1:space%dim)
724 call messages_warning(3, namespace=namespace)
725 end if
726 if (ist >= st%st_start .and. ist <= st%st_end .and. &
727 ik >= st%d%kpt%start .and. ik <= st%d%kpt%end) then
728 restart_file_present(idim, ist, ik) = .false.
729 end if
730 end if
731
732 if (read_occ) then
733 st%occ(ist, ik) = my_occ
734 integral_occs = integral_occs .and. &
735 abs((st%occ(ist, ik) - st%smear%el_per_state) * st%occ(ist, ik)) <= m_epsilon
736 end if
737 end if
738 end do
739
740 if (present(iter)) then
741 call restart%read(wfns_file, lines, 1, err)
742 if (err /= 0) then
743 ierr = ierr - 2**8
744 else
745 read(lines(1), *) filename, filename, iter
746 end if
747 end if
748
749 call restart%close(wfns_file)
750 call restart%close(occ_file)
751
752 ! At this point, the restart file should be available
753 ! if not, we fall back to the obf format
754 if (restart_file_format == option__restartfileformatstates__adios2) then
755 if (.not.loct_dir_exists(trim(restart%dir())//"/"//"wavefunctions.bp")) then
756 message(1) = "ADIOS2 restart file not found, falling back to obf format."
757 call messages_warning(1, namespace=namespace)
758 restart_file_format = option__restartfileformatstates__obf
759 end if
760 end if
761
762 ! Now we read the wavefunctions. At this point we need to have all the information from the
763 ! states, occs, and wfns files in order to avoid serialisation of reads, as restart_read
764 ! works as a barrier.
765 safe_allocate(filled(1:st%d%dim, st%st_start:st%st_end, st%d%kpt%start:st%d%kpt%end))
766 filled = .false.
767
768 if (present(lowest_missing)) lowest_missing = st%nst + 1
769
770 iread = 0
771 if (st%system_grp%is_root() .and. verbose_) then
772 idone = 0
773 ntodo = st%lnst*st%d%kpt%nlocal()*st%d%dim
774 call loct_progress_bar(-1, ntodo)
775 end if
776 if (restart_file_format == option__restartfileformatstates__obf) then
777 do ik = st%d%kpt%start, st%d%kpt%end
778 do ist = st%st_start, st%st_end
779 if (present(skip)) then
780 if (skip(ist)) cycle
781 end if
782
783 do idim = 1, st%d%dim
784
785 if (.not. restart_file_present(idim, ist, ik)) then
786 if (present(lowest_missing)) then
787 lowest_missing(idim, ik) = min(lowest_missing(idim, ik), ist)
788 end if
789 cycle
790 end if
791
792 if (states_are_real(st)) then
793 call restart%read_mesh_function(restart_file(idim, ist, ik), mesh, dpsi, err)
794 if (.not. present(lr)) then
795 call states_elec_set_state(st, mesh, idim, ist, ik, dpsi)
796 else
797 call lalg_copy(mesh%np, dpsi, lr%ddl_psi(:, idim, ist, ik))
798 end if
799 else
800 call restart%read_mesh_function(restart_file(idim, ist, ik), mesh, zpsi, err)
801 if (.not. present(lr)) then
802 call states_elec_set_state(st, mesh, idim, ist, ik, zpsi)
803 else
804 call lalg_copy(mesh%np, zpsi, lr%zdl_psi(:, idim, ist, ik))
805 end if
806 end if
807
808 if (err == 0) then
809 filled(idim, ist, ik) = .true.
810 iread = iread + 1
811 else if (present(lowest_missing)) then
812 lowest_missing(idim, ik) = min(lowest_missing(idim, ik), ist)
813 end if
814
815 if (st%system_grp%is_root() .and. verbose_) then
816 idone = idone + 1
817 call loct_progress_bar(idone, ntodo)
818 end if
819
820 end do
821 end do
822 end do
823 else if (restart_file_format == option__restartfileformatstates__adios2) then
824 if (present(lr)) then
825 ! this should never occur because we force obf format for linear response
826 call messages_not_implemented("ADIOS2 restart file format with linear response.")
827 end if
828 if (restart%has_map()) then
829 message(1) = "ADIOS2 restart file format does not support restarting from a different grid."
830 message(2) = "Please set RestartFileFormatStates = obf."
831 call messages_fatal(2, namespace=namespace)
832 end if
833 call states_elec_load_adios2(restart, st, mesh, iread, filled, restart_file_present, ierr, skip, lowest_missing)
834 end if
835
836 safe_deallocate_a(dpsi)
837 safe_deallocate_a(zpsi)
838 safe_deallocate_a(restart_file)
839 safe_deallocate_a(restart_file_present)
840
841 if (st%system_grp%is_root() .and. verbose_) then
842 call messages_new_line()
843 end if
844
845 if (st%parallel_in_states .or. st%d%kpt%parallel) then
846 iread_tmp = iread
847 call st%st_kpt_mpi_grp%allreduce(iread_tmp, iread, 1, mpi_integer, mpi_sum)
848 end if
849
850 if (st%d%kpt%parallel) then
851 ! make sure all tasks know lowest_missing over all k-points
852 if (present(lowest_missing)) then
853 safe_allocate(lowest_missing_tmp(1:st%d%dim, 1:st%nik))
854 lowest_missing_tmp = lowest_missing
855 call st%d%kpt%mpi_grp%allreduce(lowest_missing_tmp(1,1), lowest_missing(1,1), st%d%dim*st%nik, &
856 mpi_integer, mpi_min)
857 safe_deallocate_a(lowest_missing_tmp)
858 end if
859 end if
860
861 if (fixed_occ .and. iread == st%nst * st%nik * st%d%dim) then
862 ! reset to overwrite whatever smearing may have been set earlier
863 ! only do this if all states could be loaded from restart files
864 call smear_init(st%smear, namespace, st%d%ispin, fixed_occ = .true., integral_occs = integral_occs, kpoints = kpoints)
865 end if
866
867 if (.not. present(lr) .and. .not. present(skip) .and. iread /= st%nst * st%nik * st%d%dim) then
868 call states_elec_fill_random(st, mesh, kpoints, filled)
869 end if
870 ! it is better to initialize lr wfns to zero
871
872 safe_deallocate_a(filled)
873
874 if (ierr == 0 .and. iread /= st%nst * st%nik * st%d%dim) then
875 if (iread > 0) then
876 ierr = iread
877 else
878 ierr = -1
879 end if
880 ! otherwise ierr = 0 would mean either all was read correctly, or nothing at all was read!
881
882 if (.not. present(lr)) then
883 write(str, '(a,i5)') 'Reading states.'
884 else
885 write(str, '(a,i5)') 'Reading states information for linear response.'
886 end if
887 call messages_print_with_emphasis(msg=trim(str), namespace=namespace)
888 if (.not. present(skip)) then
889 write(message(1),'(a,i6,a,i6,a)') 'Only ', iread,' files out of ', &
890 st%nst * st%nik * st%d%dim, ' could be read.'
891 else
892 write(message(1),'(a,i6,a,i6,a)') 'Only ', iread,' files out of ', &
893 st%nst * st%nik * st%d%dim, ' were loaded.'
894 ierr = 0
895 end if
896 call messages_info(1, namespace=namespace)
897 call messages_print_with_emphasis(namespace=namespace)
898 end if
899
900 message(1) = 'Info: States reading done.'
901 if (verbose_) call print_date(trim(message(1))//' ')
902
903 call profiling_out("RESTART_READ")
904 pop_sub(states_elec_load)
905
906 contains
907
908 ! ---------------------------------------------------------
909
910 logical function index_is_wrong() !< .true. if the index (idim, ist, ik) is not present in st structure...
912
913 if (idim > st%d%dim .or. idim < 1 .or. &
914 ist > st%nst .or. ist < 1 .or. &
915 ik > st%nik .or. ik < 1) then
917 else
918 index_is_wrong = .false.
919 end if
920
922 end function index_is_wrong
923
924 end subroutine states_elec_load
925
926 ! ---------------------------------------------------------
931 subroutine states_elec_fill_random(st, mesh, kpoints, filled)
932 type(states_elec_t), intent(inout) :: st
933 class(mesh_t), intent(in) :: mesh
934 type(kpoints_t), intent(in) :: kpoints
935 logical, intent(in) :: filled(1:, st%st_start:, st%d%kpt%start:)
936
937 integer :: jdim, jst, jk
938 integer, allocatable :: needs_fill(:, :, :)
939
942 safe_allocate(needs_fill(1:st%d%dim, 1:st%nst, 1:st%nik))
943 needs_fill = 0
944
945 do jk = st%d%kpt%start, st%d%kpt%end
946 do jst = st%st_start, st%st_end
947 do jdim = 1, st%d%dim
948 if (.not. filled(jdim, jst, jk)) needs_fill(jdim, jst, jk) = 1
949 end do
950 end do
951 end do
952
953 call comm_allreduce(st%st_kpt_mpi_grp, needs_fill)
954
955 do jk = 1, st%nik
956 do jst = 1, st%nst
957 do jdim = 1, st%d%dim
958 if (needs_fill(jdim, jst, jk) == 0) cycle
959 call states_elec_generate_random(st, mesh, kpoints, jst, jst, jk, jk)
960 end do
961 end do
962 end do
963
964 safe_deallocate_a(needs_fill)
965
967 end subroutine states_elec_fill_random
968
969 ! ---------------------------------------------------------
970 subroutine states_elec_load_adios2(restart, st, mesh, iread, filled, restart_file_present, ierr, skip, lowest_missing)
971 type(restart_t), intent(in) :: restart
972 type(states_elec_t), target, intent(inout) :: st
973 class(mesh_t), intent(in) :: mesh
974 integer, intent(out) :: iread
975 logical, intent(out) :: filled(1:, st%st_start:, st%d%kpt%start:)
976 logical, contiguous, intent(in) :: restart_file_present(:, :, :)
977 integer, intent(out) :: ierr
978 logical, contiguous, optional, intent(in) :: skip(:)
979 integer, contiguous, optional, intent(inout) :: lowest_missing(:, :)
980
981#ifdef HAVE_ADIOS2
982 type(adios2_adios) :: adios
983 type(adios2_io) :: io
984 type(adios2_variable) :: var, var_indices
985 type(adios2_attribute) :: attribute
986 type(adios2_engine) :: engine
987 integer :: type_file, type_requested, ib, ik, ndims, pardomains
988 integer :: nst_max
989 integer(int64), allocatable :: var_shape(:)
990 integer(int64), allocatable :: global_indices(:)
991#endif
992
994#ifdef HAVE_ADIOS2
995#ifdef HAVE_MPI
996 call adios2_init(adios, restart%mpi_grp%comm%MPI_VAL, ierr)
997#else
998 call adios2_init(adios, ierr)
999#endif
1000 call check_error(.not. adios%valid .or. ierr /= adios2_error_none, "Problem initializing ADIOS2.")
1001 call adios2_declare_io(io, adios, "reader", ierr)
1002 call check_error(.not. io%valid .or. ierr /= adios2_error_none, "Problem initializing ADIOS2.")
1003 ! open restart file
1004 call adios2_open(engine, io, trim(restart%dir())//"/"//"wavefunctions.bp", adios2_mode_read, ierr)
1005 call check_error(.not. engine%valid .or. ierr /= adios2_error_none, "Problem opening ADIOS2 restart file.")
1006
1007 call adios2_begin_step(engine, ierr)
1008 ! get ParDomains from attribute
1009 call adios2_inquire_attribute(attribute, io, "ParDomains", ierr)
1010 call check_error(.not. attribute%valid .or. ierr /= adios2_error_none, "Problem inquiring ADIOS2 attribute.")
1011 call adios2_attribute_data(pardomains, attribute, ierr)
1012 if (states_are_real(st)) then
1013 type_requested = adios2_type_dp
1014 else
1015 type_requested = adios2_type_complex_dp
1016 end if
1017 call adios2_inquire_variable(var, io, 'wavefunctions', ierr)
1018 call check_error(.not. var%valid .or. ierr /= adios2_error_none, "Problem loading ADIOS2 variable (wavefunctions).")
1019 ! get variable type and shape
1020 call adios2_variable_type(type_file, var, ierr)
1021 call adios2_variable_ndims(ndims, var, ierr)
1022 call adios2_variable_shape(var_shape, ndims, var, ierr)
1023 assert(ndims == 3)
1024 nst_max = st%nst
1025 if (var_shape(1) < int(st%nst * st%d%dim, int64)) then
1026 ! more states requested than available in the restart data, only read up to nst_max
1027 nst_max = int(var_shape(1), int32) / st%d%dim
1028 end if
1029 if (var_shape(2) /= int(mesh%np_global, int64)) then
1030 ! this should never happen because we check above if there is a map which is only allocated if the grid is different
1031 message(1) = "Error: trying to restart with a different number of grid points. Not supported with ADIOS2 format."
1032 call messages_fatal(1)
1033 end if
1034 if (present(skip)) then
1035 ! read state by state if skip parameter present, otherwise batch by batch which is much better
1036 ! for the performance
1037 call states_elec_adios2_read_state_by_state(st, mesh, engine, var, type_requested, type_file, &
1038 filled, restart_file_present, iread, skip, lowest_missing)
1039 else
1040 call states_elec_adios2_read_batch_by_batch(st, mesh, engine, var, var_shape, type_requested, type_file, &
1041 nst_max, filled, iread)
1042 end if
1043 ! communicate points for mesh parallelization
1044 if (mesh%mpi_grp%size > 1 .or. pardomains > 1) then
1045 call adios2_inquire_variable(var_indices, io, 'global_indices', ierr)
1046 call check_error(.not. var_indices%valid .or. ierr /= adios2_error_none, &
1047 "Problem loading ADIOS2 variable (global_indices).")
1048 safe_allocate(global_indices(mesh%np))
1049 call adios2_set_selection(var_indices, 1, &
1050 [int(mesh%pv%xlocal-1, int64)], &
1051 [int(mesh%np, int64)], ierr)
1052 call check_error(ierr /= adios2_error_none, "Problem setting ADIOS2 selection (global_indices).")
1053 call adios2_get(engine, var_indices, global_indices, ierr)
1054 call check_error(ierr /= adios2_error_none, "Problem in ADIOS2 get (global_indices).")
1055 end if
1056 call adios2_end_step(engine, ierr)
1057 call check_error(ierr /= adios2_error_none, "Problem in ADIOS2 end_step.")
1058 if (mesh%mpi_grp%size > 1 .or. pardomains > 1) then
1059 do ik = st%d%kpt%start, st%d%kpt%end
1060 do ib = st%group%block_start, st%group%block_end
1061 if (states_are_real(st)) then
1062 call dmesh_batch_exchange_points(mesh, st%group%psib(ib, ik), forward_map=global_indices)
1063 else
1064 call zmesh_batch_exchange_points(mesh, st%group%psib(ib, ik), forward_map=global_indices)
1065 end if
1066 end do
1067 end do
1068 safe_deallocate_a(global_indices)
1069 end if
1070
1071 ! close restart file
1072 call adios2_close(engine, ierr)
1073 call check_error(ierr /= adios2_error_none, "Problem closing ADIOS2 engine.")
1074 call adios2_finalize(adios, ierr)
1075 call check_error(ierr /= adios2_error_none, "Problem finalizing ADIOS2.")
1076#else
1077 ! avoid an empty routine
1078 ierr = 0
1079#endif
1081 end subroutine states_elec_load_adios2
1082
1083
1084#ifdef HAVE_ADIOS2
1085 ! ---------------------------------------------------------
1087 subroutine states_elec_adios2_read_state_by_state(st, mesh, engine, var, type_requested, type_file, &
1088 filled, restart_file_present, iread, skip, lowest_missing)
1089 type(states_elec_t), target, intent(inout) :: st
1090 class(mesh_t), intent(in) :: mesh
1091 type(adios2_engine), intent(inout) :: engine
1092 type(adios2_variable), intent(inout) :: var
1093 integer, intent(in) :: type_requested
1094 integer, intent(in) :: type_file
1095 logical, intent(inout) :: filled(1:, st%st_start:, st%d%kpt%start:)
1096 logical, contiguous, intent(in) :: restart_file_present(:, :, :)
1097 integer, intent(inout) :: iread
1098 logical, contiguous, optional, intent(in) :: skip(:)
1099 integer, contiguous, optional, intent(inout) :: lowest_missing(:, :)
1100
1101 integer :: ik, ist, idim, ierr
1102 real(real64), allocatable :: dpsi(:)
1103 complex(real64), allocatable :: zpsi(:)
1104
1105 push_sub(states_elec_adios2_read_state_by_state)
1106
1107 if (type_requested == adios2_type_dp .and. type_file == adios2_type_complex_dp) then
1108 message(1) = "Error: calculation requested real states for restarting, but restart file has complex states."
1109 call messages_fatal(1)
1110 end if
1111
1112 do ik = st%d%kpt%start, st%d%kpt%end
1113 ! the buffer handed to adios2_get must match the type stored in the file,
1114 ! which is not necessarily the type of the states we are reading into
1115 if (type_file == adios2_type_dp) then
1116 safe_allocate(dpsi(1:mesh%np))
1117 else
1118 safe_allocate(zpsi(1:mesh%np))
1119 end if
1120 do ist = st%st_start, st%st_end
1121 if (present(skip)) then
1122 if (skip(ist)) cycle
1123 end if
1124
1125 do idim = 1, st%d%dim
1126 if (.not. restart_file_present(idim, ist, ik)) then
1127 if (present(lowest_missing)) then
1128 lowest_missing(idim, ik) = min(lowest_missing(idim, ik), ist)
1129 end if
1130 cycle
1131 end if
1132
1133 ! read only one state
1134 call adios2_set_selection(var, 3, &
1135 [int((ist-1)*st%d%dim+idim-1, int64), int(mesh%pv%xlocal-1, int64), int(ik-1, int64)], &
1136 [1_int64, int(mesh%np, int64), 1_int64], ierr)
1137 ! use sync mode here to immediately read into dpsi/zpsi
1138 if (type_file == adios2_type_dp) then
1139 call adios2_get(engine, var, dpsi, adios2_mode_sync, ierr)
1140 call check_error(ierr /= adios2_error_none, "Problem in ADIOS2 get dpsi.")
1141 if (type_requested == adios2_type_complex_dp) then
1142 ! real restart data read into complex states
1143 call states_elec_set_state(st, mesh, idim, ist, ik, cmplx(dpsi, m_zero, real64))
1144 else
1145 call states_elec_set_state(st, mesh, idim, ist, ik, dpsi)
1146 end if
1147 else
1148 call adios2_get(engine, var, zpsi, adios2_mode_sync, ierr)
1149 call check_error(ierr /= adios2_error_none, "Problem in ADIOS2 get zpsi.")
1150 call states_elec_set_state(st, mesh, idim, ist, ik, zpsi)
1151 end if
1152
1153 filled(idim, ist, ik) = .true.
1154 iread = iread + 1
1155 end do
1156 end do
1157 safe_deallocate_a(dpsi)
1158 safe_deallocate_a(zpsi)
1159 end do
1160
1161 pop_sub(states_elec_adios2_read_state_by_state)
1162 end subroutine states_elec_adios2_read_state_by_state
1163
1164 ! ---------------------------------------------------------
1166 subroutine states_elec_adios2_read_batch_by_batch(st, mesh, engine, var, var_shape, type_requested, type_file, &
1167 nst_max, filled, iread)
1168 type(states_elec_t), target, intent(inout) :: st
1169 class(mesh_t), intent(in) :: mesh
1170 type(adios2_engine), intent(inout) :: engine
1171 type(adios2_variable), intent(inout) :: var
1172 integer(int64), contiguous, intent(in) :: var_shape(:)
1173 integer, intent(in) :: type_requested
1174 integer, intent(in) :: type_file
1175 integer, intent(in) :: nst_max
1176 logical, intent(inout) :: filled(1:, st%st_start:, st%d%kpt%start:)
1177 integer, intent(inout) :: iread
1178
1179 integer :: ib, ik, ist, ip, adios2_mode, nst_linear, ierr
1180 class(wfs_elec_t), pointer :: psib
1181 real(real64), allocatable :: dff_pack(:, :)
1182
1183 push_sub(states_elec_adios2_read_batch_by_batch)
1184
1185 do ik = st%d%kpt%start, st%d%kpt%end
1186 ! if there are more k points requested than available, skip the ones that are not available
1187 ! this can, e.g., happen for unocc calculations with a k points path
1188 if (ik > var_shape(3)) cycle
1189 do ib = st%group%block_start, st%group%block_end
1190 select case (st%group%psib(ib, ik)%status())
1191 case (batch_not_packed)
1192 safe_allocate(psib)
1193 call st%group%psib(ib, ik)%copy_to(psib, copy_data=.false.)
1194 call psib%do_pack(batch_packed, copy=.false.)
1195 adios2_mode = adios2_mode_sync
1196 case (batch_packed)
1197 psib => st%group%psib(ib, ik)
1198 !adios2_mode = adios2_mode_deferred
1199 ! switch to sync mode for the time being, seems to solve some errors
1200 ! at the moment, it is unclear why
1201 adios2_mode = adios2_mode_sync
1202 case (batch_device_packed)
1203 safe_allocate(psib)
1204 call st%group%psib(ib, ik)%copy_to(psib)
1205 call psib%do_unpack(force=.true., copy=.false.)
1206 ! make sure we have a packed batch
1207 call psib%do_pack(batch_packed)
1208 adios2_mode = adios2_mode_sync
1209 end select
1210 assert(psib%status() == batch_packed)
1211
1212 ! read only up to the highest available state in the restart data
1213 nst_linear = st%group%psib(ib, ik)%nst_linear
1214 if (states_elec_block_max(st, ib) > nst_max) then
1215 nst_linear = (nst_max - states_elec_block_min(st, ib) + 1) * st%d%dim
1216 end if
1217 if (nst_linear <= 0) then
1218 select case (st%group%psib(ib, ik)%status())
1219 case (batch_not_packed)
1220 call psib%end()
1221 safe_deallocate_p(psib)
1222 case (batch_packed)
1223 nullify(psib)
1224 case (batch_device_packed)
1225 call psib%end()
1226 safe_deallocate_p(psib)
1227 end select
1228 cycle
1229 end if
1230
1231 ! select part of variable to write, correspond to the memory of the current batch
1232 ! first argument gives the start in the global array
1233 ! second argument gives the size of the local batch
1234 call adios2_set_selection(var, 3, &
1235 [int((states_elec_block_min(st, ib)-1)*st%d%dim, int64), int(mesh%pv%xlocal-1, int64), int(ik-1, int64)], &
1236 [int(nst_linear, int64), int(mesh%np, int64), 1_int64], ierr)
1237 call check_error(ierr /= adios2_error_none, "Problem setting ADIOS2 selection.")
1238
1239 if (type_requested == adios2_type_dp .and. type_file == adios2_type_dp) then
1240 ! set memory layout of one batch, take into account padding and ghost/boundary points
1241 call adios2_set_memory_selection(var, 3, [0_int64, 0_int64, 0_int64], &
1242 [int(size(psib%dff_pack, 1), int64), &
1243 int(size(psib%dff_pack, 2), int64), 1_int64], ierr)
1244 call check_error(ierr /= adios2_error_none, "Problem setting ADIOS2 memory selection (reading real).")
1245
1246 call adios2_get(engine, var, psib%dff_pack, adios2_mode, ierr)
1247 call check_error(ierr /= adios2_error_none, "Problem in ADIOS2 get (reading real).")
1248 else if (type_requested == adios2_type_complex_dp .and. type_file == adios2_type_complex_dp) then
1249 ! set memory layout of one batch, take into account padding and ghost/boundary points
1250 call adios2_set_memory_selection(var, 3, [0_int64, 0_int64, 0_int64], &
1251 [int(size(psib%zff_pack, 1), int64), &
1252 int(size(psib%zff_pack, 2), int64), 1_int64], ierr)
1253 call check_error(ierr /= adios2_error_none, "Problem setting ADIOS2 memory selection (reading complex).")
1254
1255 call adios2_get(engine, var, psib%zff_pack, adios2_mode, ierr)
1256 call check_error(ierr /= adios2_error_none, "Problem in ADIOS2 get (reading complex).")
1257 else if (type_requested == adios2_type_complex_dp .and. type_file == adios2_type_dp) then
1258 ! read into a local real variable and copy to the complex batch buffer
1259 safe_allocate(dff_pack(size(psib%zff_pack, 1), size(psib%zff_pack, 2)))
1260 ! set memory layout of one batch, take into account padding and ghost/boundary points
1261 call adios2_set_memory_selection(var, 3, [0_int64, 0_int64, 0_int64], &
1262 [int(size(psib%zff_pack, 1), int64), &
1263 int(size(psib%zff_pack, 2), int64), 1_int64], ierr)
1264 call check_error(ierr /= adios2_error_none, "Problem setting ADIOS2 memory selection (reading complex from real).")
1265
1266 ! use sync mode here to immediately read into dff_pack
1267 call adios2_get(engine, var, dff_pack, adios2_mode_sync, ierr)
1268 call check_error(ierr /= adios2_error_none, "Problem in ADIOS2 get (reading complex from real).")
1269 ! copy to batch memory
1270 !$omp parallel do private(ist)
1271 do ip = 1, mesh%np
1272 do ist = 1, nst_linear
1273 psib%zff_pack(ist, ip) = cmplx(dff_pack(ist, ip), m_zero, real64)
1274 end do
1275 end do
1276 safe_deallocate_a(dff_pack)
1277 else
1278 message(1) = "Error: requested to read complex states for restarting, but calculation has real states."
1279 call messages_fatal(1)
1280 end if
1281 ! make sure iread and filled are set correctly
1282 iread = iread + nst_linear
1283 do ist = 1, nst_linear
1284 filled(st%group%psib(ib, ik)%linear_to_idim(ist), st%group%psib(ib, ik)%linear_to_ist(ist), ik) = .true.
1285 end do
1286
1287 select case (st%group%psib(ib, ik)%status())
1288 case (batch_not_packed)
1289 call psib%do_unpack()
1290 call psib%copy_data_to(mesh%np, st%group%psib(ib, ik))
1291 call psib%end()
1292 safe_deallocate_p(psib)
1293 case (batch_packed)
1294 nullify(psib)
1295 case (batch_device_packed)
1296 call psib%do_pack()
1297 call psib%copy_data_to(mesh%np, st%group%psib(ib, ik))
1298 call psib%end()
1299 safe_deallocate_p(psib)
1300 end select
1301 end do
1302 end do
1303
1304 pop_sub(states_elec_adios2_read_batch_by_batch)
1305 end subroutine states_elec_adios2_read_batch_by_batch
1306
1307 ! ---------------------------------------------------------
1308 ! only used for adios2 error checking
1309 subroutine check_error(condition, error_message)
1310 logical, intent(in) :: condition
1311 character(len=*), intent(in) :: error_message
1312 if (condition) then
1313 message(1) = error_message
1314 call messages_fatal(1)
1315 end if
1316 end subroutine check_error
1317#endif
1318
1319 subroutine states_elec_dump_rho(restart, st, mesh, ierr, iter)
1320 type(restart_t), intent(in) :: restart
1321 type(states_elec_t), intent(in) :: st
1322 class(mesh_t), intent(in) :: mesh
1323 integer, intent(out) :: ierr
1324 integer, optional, intent(in) :: iter
1325
1326 integer :: iunit, isp, err, err2(2)
1327 character(len=MAX_PATH_LEN) :: filename
1328 character(len=300) :: lines(2)
1329
1330 push_sub(states_elec_dump_rho)
1331
1332 ierr = 0
1333
1334 if (restart%skip()) then
1335 pop_sub(states_elec_dump_rho)
1336 return
1337 end if
1338
1339 message(1) = "Debug: Writing density restart."
1340 call messages_info(1, debug_only=.true.)
1341
1343
1344 !write the densities
1345 iunit = restart%open('density')
1346 lines(1) = '# #spin #nspin filename'
1347 lines(2) = '%densities'
1348 call restart%write(iunit, lines, 2, err)
1349 if (err /= 0) ierr = ierr + 1
1350
1351 err2 = 0
1352 do isp = 1, st%d%nspin
1353 filename = get_filename_with_spin('density', st%d%nspin, isp)
1354 write(lines(1), '(i8,a,i8,a)') isp, ' | ', st%d%nspin, ' | "'//trim(adjustl(filename))//'"'
1355 call restart%write(iunit, lines, 1, err)
1356 if (err /= 0) err2(1) = err2(1) + 1
1357
1358 call restart%write_mesh_function(filename, mesh, st%rho(:,isp), err)
1359 if (err /= 0) err2(2) = err2(2) + 1
1360
1361 end do
1362 if (err2(1) /= 0) ierr = ierr + 2
1363 if (err2(2) /= 0) ierr = ierr + 4
1364
1365 lines(1) = '%'
1366 call restart%write(iunit, lines, 1, err)
1367 if (err /= 0) ierr = ierr + 8
1368 if (present(iter)) then
1369 write(lines(1),'(a,i7)') 'Iter = ', iter
1370 call restart%write(iunit, lines, 1, err)
1371 if (err /= 0) ierr = ierr + 16
1372 end if
1373 call restart%close(iunit)
1374
1376
1377 message(1) = "Debug: Writing density restart done."
1378 call messages_info(1, debug_only=.true.)
1379
1380 pop_sub(states_elec_dump_rho)
1381 end subroutine states_elec_dump_rho
1382
1383
1384 ! ---------------------------------------------------------
1385 subroutine states_elec_load_rho(restart, st, mesh, ierr)
1386 type(restart_t), intent(in) :: restart
1387 type(states_elec_t), intent(inout) :: st
1388 class(mesh_t), intent(in) :: mesh
1389 integer, intent(out) :: ierr
1390
1391 integer :: err, err2, isp
1392 character(len=MAX_PATH_LEN) :: filename
1393
1394 push_sub(states_elec_load_rho)
1395
1396 ierr = 0
1397
1398 if (restart%skip()) then
1399 ierr = -1
1400 pop_sub(states_elec_load_rho)
1401 return
1402 end if
1403
1404 message(1) = "Debug: Reading density restart."
1405 call messages_info(1, debug_only=.true.)
1406
1407 ! skip for now, since we know what the files are going to be called
1408 !read the densities
1409! iunit_rho = io_open(trim(dir)//'/density', restart%namespace, action='write')
1410! call iopar_read(st%dom_st_kpt_mpi_grp, iunit_rho, line, err)
1411! call iopar_read(st%dom_st_kpt_mpi_grp, iunit_rho, line, err)
1412! we could read the iteration 'iter' too, not sure if that is useful.
1413
1414 err2 = 0
1415 do isp = 1, st%d%nspin
1416 filename = get_filename_with_spin('density', st%d%nspin, isp)
1417! if (st%dom_st_kpt_mpi_grp%is_root()) then
1418! read(iunit_rho, '(i8,a,i8,a)') isp, ' | ', st%d%nspin, ' | "'//trim(adjustl(filename))//'"'
1419! end if
1420 call restart%read_mesh_function(filename, mesh, st%rho(:,isp), err)
1421 if (err /= 0) err2 = err2 + 1
1422
1423 end do
1424 if (err2 /= 0) ierr = ierr + 1
1425
1426 message(1) = "Debug: Reading density restart done."
1427 call messages_info(1, debug_only=.true.)
1428
1430 end subroutine states_elec_load_rho
1431
1432 subroutine states_elec_dump_frozen(restart, space, st, mesh, ierr)
1433 type(restart_t), intent(in) :: restart
1434 class(space_t), intent(in) :: space
1435 type(states_elec_t), intent(in) :: st
1436 class(mesh_t), intent(in) :: mesh
1437 integer, intent(out) :: ierr
1438
1439 integer :: isp, err, err2(2), idir
1440 character(len=MAX_PATH_LEN) :: filename
1441
1442 push_sub(states_elec_dump_frozen)
1443
1444 ierr = 0
1445
1446 assert(allocated(st%frozen_rho))
1447
1448 if (restart%skip()) then
1450 return
1451 end if
1452
1453 message(1) = "Debug: Writing frozen densities restart."
1454 call messages_info(1, debug_only=.true.)
1455
1457
1458 err2 = 0
1459 do isp = 1, st%d%nspin
1460 filename = get_filename_with_spin('frozen_rho', st%d%nspin, isp)
1461
1462 call restart%write_mesh_function(filename, mesh, st%frozen_rho(:,isp), err)
1463 if (err /= 0) err2(2) = err2(2) + 1
1464
1465 if (allocated(st%frozen_tau)) then
1466 filename = get_filename_with_spin('frozen_tau', st%d%nspin, isp)
1467 call restart%write_mesh_function(filename, mesh, st%frozen_tau(:,isp), err)
1468 if (err /= 0) err2 = err2 + 1
1469 end if
1470
1471 if (allocated(st%frozen_gdens)) then
1472 do idir = 1, space%dim
1473 if (st%d%nspin == 1) then
1474 write(filename, fmt='(a,i1)') 'frozen_gdens-dir', idir
1475 else
1476 write(filename, fmt='(a,i1,a,i1)') 'frozen_gdens-dir', idir, '-', isp
1477 end if
1478 call restart%write_mesh_function(filename, mesh, st%frozen_gdens(:,idir,isp), err)
1479 if (err /= 0) err2 = err2 + 1
1480 end do
1481 end if
1482
1483 if (allocated(st%frozen_ldens)) then
1484 filename = get_filename_with_spin('frozen_ldens', st%d%nspin, isp)
1485 call restart%write_mesh_function(filename, mesh, st%frozen_ldens(:,isp), err)
1486 if (err /= 0) err2 = err2 + 1
1487 end if
1488
1489 end do
1490 if (err2(1) /= 0) ierr = ierr + 2
1491 if (err2(2) /= 0) ierr = ierr + 4
1492
1494
1495 message(1) = "Debug: Writing frozen densities restart done."
1496 call messages_info(1, debug_only=.true.)
1497
1499 end subroutine states_elec_dump_frozen
1500
1501
1502 ! ---------------------------------------------------------
1503 subroutine states_elec_load_frozen(restart, space, st, mesh, ierr)
1504 type(restart_t), intent(in) :: restart
1505 class(space_t), intent(in) :: space
1506 type(states_elec_t), intent(inout) :: st
1507 class(mesh_t), intent(in) :: mesh
1508 integer, intent(out) :: ierr
1509
1510 integer :: err, err2, isp, idir
1511 character(len=MAX_PATH_LEN) :: filename
1512
1513 push_sub(states_elec_load_frozen)
1514
1515 assert(allocated(st%frozen_rho))
1516
1517 ierr = 0
1518
1519 if (restart%skip()) then
1520 ierr = -1
1522 return
1523 end if
1524
1525 message(1) = "Debug: Reading densities restart."
1526 call messages_info(1, debug_only=.true.)
1527
1528 err2 = 0
1529 do isp = 1, st%d%nspin
1530 filename = get_filename_with_spin('frozen_rho', st%d%nspin, isp)
1531 call restart%read_mesh_function(filename, mesh, st%frozen_rho(:,isp), err)
1532 if (err /= 0) err2 = err2 + 1
1533
1534 if (allocated(st%frozen_tau)) then
1535 filename = get_filename_with_spin('frozen_tau', st%d%nspin, isp)
1536 call restart%read_mesh_function(filename, mesh, st%frozen_tau(:,isp), err)
1537 if (err /= 0) err2 = err2 + 1
1538 end if
1539
1540 if (allocated(st%frozen_gdens)) then
1541 do idir = 1, space%dim
1542 if (st%d%nspin == 1) then
1543 write(filename, fmt='(a,i1)') 'frozen_gdens-dir', idir
1544 else
1545 write(filename, fmt='(a,i1,a,i1)') 'frozen_gdens-dir', idir, '-', isp
1546 end if
1547 call restart%read_mesh_function(filename, mesh, st%frozen_gdens(:,idir,isp), err)
1548 if (err /= 0) err2 = err2 + 1
1549 end do
1550 end if
1551
1552 if (allocated(st%frozen_ldens)) then
1553 filename = get_filename_with_spin('frozen_ldens', st%d%nspin, isp)
1554 call restart%read_mesh_function(filename, mesh, st%frozen_ldens(:,isp), err)
1555 if (err /= 0) err2 = err2 + 1
1556 end if
1557
1558 end do
1559 if (err2 /= 0) ierr = ierr + 1
1560
1561 message(1) = "Debug: Reading frozen densities restart done."
1562 call messages_info(1, debug_only=.true.)
1563
1565 end subroutine states_elec_load_frozen
1566
1567
1568 ! ---------------------------------------------------------
1571 subroutine states_elec_read_user_def_orbitals(mesh, namespace, space, st)
1572 class(mesh_t), intent(in) :: mesh
1573 type(namespace_t), intent(in) :: namespace
1574 class(space_t), intent(in) :: space
1575 type(states_elec_t), intent(inout) :: st
1576
1577 type(block_t) :: blk
1578 integer :: ip, id, is, ik, nstates, state_from, ierr, ncols
1579 integer :: ib, idim, inst, inik, normalize
1580 real(real64) :: xx(space%dim), rr, psi_re, psi_im
1581 character(len=150) :: filename
1582 complex(real64), allocatable :: zpsi(:, :)
1583
1584 integer, parameter :: &
1585 state_from_formula = 1, &
1586 state_from_file = -10010, &
1587 normalize_yes = 1, &
1588 normalize_no = 0
1589
1591
1592 !%Variable UserDefinedStates
1593 !%Type block
1594 !%Section States
1595 !%Description
1596 !% Instead of using the ground state as initial state for
1597 !% time-propagations it might be interesting in some cases
1598 !% to specify alternate states. Like with user-defined
1599 !% potentials, this block allows you to specify formulas for
1600 !% the orbitals at <i>t</i>=0.
1601 !%
1602 !% Example:
1603 !%
1604 !% <tt>%UserDefinedStates
1605 !% <br>&nbsp;&nbsp; 1 | 1 | 1 | formula | "exp(-r^2)*exp(-i*0.2*x)" | normalize_yes
1606 !% <br>%</tt>
1607 !%
1608 !% The first column specifies the component of the spinor,
1609 !% the second column the number of the state and the third
1610 !% contains <i>k</i>-point and spin quantum numbers. Column four
1611 !% indicates that column five should be interpreted as a formula
1612 !% for the corresponding orbital.
1613 !%
1614 !% Alternatively, if column four states <tt>file</tt> the state will
1615 !% be read from the file given in column five.
1616 !%
1617 !% <tt>%UserDefinedStates
1618 !% <br>&nbsp;&nbsp; 1 | 1 | 1 | file | "/path/to/file" | normalize_no
1619 !% <br>%</tt>
1620 !%
1621 !% Octopus reads first the ground-state orbitals from
1622 !% the <tt>restart/gs</tt> directory. Only the states that are
1623 !% specified in the above block will be overwritten with
1624 !% the given analytic expression for the orbital.
1625 !%
1626 !% The sixth (optional) column indicates whether <tt>Octopus</tt> should renormalize
1627 !% the orbital. The default (no sixth column given) is to renormalize.
1628 !%
1629 !%Option file -10010
1630 !% Read initial orbital from file.
1631 !% Accepted file formats, detected by extension: obf, ncdf and csv (real only).
1632 !% For csv files, the following formatting rules apply:
1633 !%
1634 !% The functions in this file read an array from an ascii matrix (csv) file.
1635 !% Format with values "valueXYZ" as follows
1636 !%
1637 !% File values.csv:
1638 !% --------
1639 !% value111 value211 value311 value411
1640 !% value121 value221 value321 value421
1641 !% value131 value231 value331 value431
1642 !%
1643 !% value112 value212 value312 value412
1644 !% value122 value222 value322 value422
1645 !% value132 value232 value332 value432
1646 !%
1647 !% value113 value213 value313 value413
1648 !% value123 value223 value323 value423
1649 !% value133 value233 value333 value433
1650 !% --------
1651 !%
1652 !% That is, every line scans the x-coordinate, every XY-plane as a table of values
1653 !% and all XY-planes separated by an empty row.
1654 !%
1655 !% The given matrix is interpolated/stretched to fit the calculation
1656 !% box defined in input file.
1657 !%
1658 !% Note that there must not be any empty line at the end of the file.
1659 !%
1660 !% Calculation box shape must be "parallelepiped".
1661 !%
1662 !% The delimiter can be a tab, a comma or a space.
1663 !%Option formula 1
1664 !% Calculate initial orbital by given analytic expression.
1665 !%Option normalize_yes 1
1666 !% Normalize orbitals (default).
1667 !%Option normalize_no 0
1668 !% Do not normalize orbitals.
1669 !%End
1670 if (parse_block(namespace, 'UserDefinedStates', blk) == 0) then
1671
1672 call messages_print_with_emphasis(msg=trim('Substitution of orbitals'), namespace=namespace)
1673
1674 ! find out how many lines (i.e. states) the block has
1675 nstates = parse_block_n(blk)
1676
1677 safe_allocate(zpsi(1:mesh%np, 1:st%d%dim))
1678
1679 ! read all lines
1680 do ib = 1, nstates
1681 ! Check that number of columns is five or six.
1682 ncols = parse_block_cols(blk, ib - 1)
1683 if (ncols < 5 .or. ncols > 6) then
1684 message(1) = 'Each line in the UserDefinedStates block must have'
1685 message(2) = 'five or six columns.'
1686 call messages_fatal(2, namespace=namespace)
1687 end if
1688
1689 call parse_block_integer(blk, ib - 1, 0, idim)
1690 call parse_block_integer(blk, ib - 1, 1, inst)
1691 call parse_block_integer(blk, ib - 1, 2, inik)
1692
1693 ! Calculate from expression or read from file?
1694 call parse_block_integer(blk, ib - 1, 3, state_from)
1695
1696 ! loop over all states
1697 do id = 1, st%d%dim
1698 do is = 1, st%nst
1699 do ik = 1, st%nik
1700
1701 if (.not.(id == idim .and. is == inst .and. ik == inik )) cycle
1702
1703 ! We first parse the value, such that the parsing and stdout looks correct
1704 ! However, if rank 0 is not responsible for the parsing, the variables in the formula
1705 ! will still appear as not parsed (pure k-point/state parallelization case).
1706 ! This could be solved by a dummy call to parse_expression
1707 select case (state_from)
1708 case (state_from_formula)
1709 ! parse formula string
1710 call parse_block_string( &
1711 blk, ib - 1, 4, st%user_def_states(id, is, ik))
1712
1713 write(message(1), '(a,3i5)') 'Substituting state of orbital with k, ist, dim = ', ik, is, id
1714 write(message(2), '(2a)') ' with the expression:'
1715 write(message(3), '(2a)') ' ',trim(st%user_def_states(id, is, ik))
1716 call messages_info(3, namespace=namespace)
1717
1718 ! convert to C string
1719 call conv_to_c_string(st%user_def_states(id, is, ik))
1720
1721 case (state_from_file)
1722 ! The input format can be coded in column four now. As it is
1723 ! not used now, we just say "file".
1724 ! Read the filename.
1725 call parse_block_string(blk, ib - 1, 4, filename)
1726
1727 write(message(1), '(a,3i5)') 'Substituting state of orbital with k, ist, dim = ', ik, is, id
1728 write(message(2), '(2a)') ' with data from file:'
1729 write(message(3), '(2a)') ' ',trim(filename)
1730 call messages_info(3, namespace=namespace)
1731 end select
1732
1733
1734 ! does the block entry match and is this node responsible?
1735 if (.not.(st%st_start <= is .and. st%st_end >= is &
1736 .and. st%d%kpt%start <= ik .and. st%d%kpt%end >= ik)) cycle
1737
1738 select case (state_from)
1739
1740 case (state_from_formula)
1741 ! fill states with user-defined formulas
1742 do ip = 1, mesh%np
1743 xx = mesh%x(:, ip)
1744 rr = norm2(xx)
1745
1746 ! parse user-defined expressions
1747 call parse_expression(psi_re, psi_im, space%dim, xx, rr, m_zero, st%user_def_states(id, is, ik))
1748 ! fill state
1749 zpsi(ip, 1) = cmplx(psi_re, psi_im, real64)
1750 end do
1751
1752 case (state_from_file)
1753 ! finally read the state
1754 call zio_function_input(filename, namespace, space, mesh, zpsi(:, 1), ierr)
1755 if (ierr > 0) then
1756 message(1) = 'Could not read the file!'
1757 write(message(2),'(a,i1)') 'Error code: ', ierr
1758 call messages_fatal(2, namespace=namespace)
1759 end if
1760
1761 case default
1762 message(1) = 'Wrong entry in UserDefinedStates, column 4.'
1763 message(2) = 'You may state "formula" or "file" here.'
1764 call messages_fatal(2, namespace=namespace)
1765 end select
1766
1767 ! normalize orbital
1768 if (parse_block_cols(blk, ib - 1) == 6) then
1769 call parse_block_integer(blk, ib - 1, 5, normalize)
1770 else
1771 normalize = 1
1772 end if
1773 select case (normalize)
1774 case (normalize_no)
1775 call states_elec_set_state(st, mesh, id, is, ik, zpsi(:, 1))
1776 case (normalize_yes)
1777 assert(st%d%dim == 1)
1778 call zmf_normalize(mesh, st%d%dim, zpsi)
1779 call states_elec_set_state(st, mesh, is, ik, zpsi)
1780 case default
1781 message(1) = 'The sixth column in UserDefinedStates may either be'
1782 message(2) = '"normalize_yes" or "normalize_no"'
1783 call messages_fatal(2, namespace=namespace)
1784 end select
1785
1786 end do
1787 end do
1788 end do
1789
1790 end do
1791
1792 safe_deallocate_a(zpsi)
1793
1794 call parse_block_end(blk)
1795 call messages_print_with_emphasis(namespace=namespace)
1796
1797 else
1798 call messages_variable_is_block(namespace, 'UserDefinedStates')
1799 end if
1800
1803
1804 ! ---------------------------------------------------------
1805 ! This is needed as for the generalized Bloch theorem we need to label
1806 ! the states from the expectation value of Sz computed from the GS.
1807 subroutine states_elec_dump_spin(restart, st, ierr)
1808 type(restart_t), intent(in) :: restart
1809 type(states_elec_t), intent(in) :: st
1810 integer, intent(out) :: ierr
1811
1812 integer :: iunit_spin
1813 integer :: err, err2(2), ik, ist
1814 character(len=300) :: lines(3)
1815
1816 push_sub(states_elec_dump_spin)
1817
1818 ierr = 0
1819
1820 if (restart%skip()) then
1821 pop_sub(states_elec_dump_spin)
1822 return
1823 end if
1824
1825 call profiling_in("RESTART_WRITE_SPIN")
1826
1828
1829 iunit_spin = restart%open('spin')
1830 lines(1) = '# #k-point #st #spin(x) spin(y) spin(z)'
1831 call restart%write(iunit_spin, lines, 1, err)
1832 if (err /= 0) ierr = ierr + 1
1833
1834 err2 = 0
1835 do ik = 1, st%nik
1836 do ist = 1, st%nst
1837 write(lines(1), '(i8,a,i8,3(a,f18.12))') ik, ' | ', ist, ' | ', &
1838 st%spin(1,ist,ik), ' | ', st%spin(2,ist,ik),' | ', st%spin(3,ist,ik)
1839 call restart%write(iunit_spin, lines, 1, err)
1840 if (err /= 0) err2(1) = err2(1) + 1
1841 end do ! st%nst
1842 end do ! st%nik
1843 lines(1) = '%'
1844 call restart%write(iunit_spin, lines, 1, err)
1845 if (err2(1) /= 0) ierr = ierr + 8
1846 if (err2(2) /= 0) ierr = ierr + 16
1847
1848 call restart%close(iunit_spin)
1849
1851
1852 call profiling_out("RESTART_WRITE_SPIN")
1853 pop_sub(states_elec_dump_spin)
1854 end subroutine states_elec_dump_spin
1855
1856
1857
1858
1859 ! ---------------------------------------------------------
1864 subroutine states_elec_load_spin(restart, st, ierr)
1865 type(restart_t), intent(in) :: restart
1866 type(states_elec_t), intent(inout) :: st
1867 integer, intent(out) :: ierr
1868
1869 integer :: spin_file, err, ik, ist
1870 character(len=256) :: lines(3)
1871 real(real64) :: spin(3)
1872 character(len=1) :: char
1873
1874
1875 push_sub(states_elec_load_spin)
1876
1877 ierr = 0
1878
1879 if (restart%skip()) then
1880 ierr = -1
1881 pop_sub(states_elec_load_spin)
1882 return
1883 end if
1884
1885 call profiling_in("RESTART_READ_SPIN")
1886
1887 ! open files to read
1888 spin_file = restart%open('spin')
1889 ! Skip two lines.
1890 call restart%read(spin_file, lines, 1, err)
1891 if (err /= 0) ierr = ierr - 2**7
1892
1893 ! If any error occurred up to this point then it is not worth continuing,
1894 ! as there something fundamentally wrong with the restart files
1895 if (ierr /= 0) then
1896 call restart%close(spin_file)
1897 call profiling_out("RESTART_READ_SPIN")
1898 pop_sub(states_elec_load_spin)
1899 return
1900 end if
1901
1902 ! Next we read the list of states from the files.
1903 ! Errors in reading the information of a specific state from the files are ignored
1904 ! at this point, because later we will skip reading the wavefunction of that state.
1905 do
1906 call restart%read(spin_file, lines, 1, err)
1907 if (err /= 0) cycle
1908
1909 read(lines(1), '(a)') char
1910 if (char == '%') then
1911 !We reached the end of the file
1912 exit
1913 else
1914 read(lines(1), *) ik, char, ist, char, spin(1), char, spin(2), char, spin(3)
1915 end if
1916
1917 st%spin(1:3, ist, ik) = spin(1:3)
1918 end do
1919
1920 call restart%close(spin_file)
1921
1922 call profiling_out("RESTART_READ_SPIN")
1923 pop_sub(states_elec_load_spin)
1924 end subroutine states_elec_load_spin
1925
1926 ! ---------------------------------------------------------
1927 subroutine states_elec_transform(st, namespace, space, restart, mesh, kpoints, prefix)
1928 type(states_elec_t), intent(inout) :: st
1929 type(namespace_t), intent(in) :: namespace
1930 class(space_t), intent(in) :: space
1931 type(restart_t), intent(inout) :: restart
1932 class(mesh_t), intent(in) :: mesh
1933 type(kpoints_t), intent(in) :: kpoints
1934 character(len=*), optional, intent(in) :: prefix
1935
1936 type(states_elec_t) :: stin
1937 type(block_t) :: blk
1938 complex(real64), allocatable :: rotation_matrix(:,:), psi(:, :)
1939 integer :: ist, jst, ncols, iqn
1940 character(len=256) :: block_name
1941
1942 push_sub(states_elec_transform)
1943
1944 block_name = trim(optional_default(prefix, "")) // "TransformStates"
1945
1946 !%Variable TransformStates
1947 !%Type block
1948 !%Default no
1949 !%Section States
1950 !%Description
1951 !% Before starting the <tt>td</tt> calculation, the initial states (that are
1952 !% read from the <tt>restart/gs</tt> directory, which should have been
1953 !% generated in a previous ground-state calculation) can be "transformed"
1954 !% among themselves. The block <tt>TransformStates</tt> gives the transformation matrix
1955 !% to be used. The number of rows and columns of the matrix should equal the number
1956 !% of the states present in the time-dependent calculation (the independent
1957 !% spin and <i>k</i>-point subspaces are all transformed equally); the number of
1958 !% columns should be equal to the number of states present in the
1959 !% <tt>restart/gs</tt> directory. This number may be different: for example,
1960 !% one could have run previously in <tt>unocc</tt> mode in order to obtain unoccupied
1961 !% Kohn-Sham states, and therefore <tt>restart/gs</tt> will contain more states.
1962 !% These states can be used in the transformation.
1963 !%
1964 !% Note that the code will not check the orthonormality of the new states!
1965 !% Occupations are also fixed to the one of the previous states
1966 !%
1967 !% Each line provides the coefficients of the new states, in terms of
1968 !% the old ones. The coefficients are complex, but the imaginary part will be
1969 !% ignored for real wavefunctions.
1970 !% Note: This variable cannot be used when parallel in states.
1971 !%End
1972 if (parse_is_defined(namespace, trim(block_name))) then
1973 if (parse_block(namespace, trim(block_name), blk) == 0) then
1974 if (st%parallel_in_states) then
1975 call messages_not_implemented(trim(block_name) // " parallel in states", namespace=namespace)
1976 end if
1977 if (parse_block_n(blk) /= st%nst) then
1978 message(1) = "Number of rows in block " // trim(block_name) // " must equal number of states in this calculation."
1979 call messages_fatal(1, namespace=namespace)
1980 end if
1981 call states_elec_copy(stin, st, exclude_wfns = .true.)
1982 call states_elec_look_and_load(restart, namespace, space, stin, mesh, kpoints, fixed_occ = .true.)
1983
1984 ! FIXME: rotation matrix should be R_TYPE
1985 safe_allocate(rotation_matrix(1:stin%nst, 1:stin%nst))
1986 safe_allocate(psi(1:mesh%np, 1:st%d%dim))
1987
1988 rotation_matrix = diagonal_matrix(stin%nst, m_z1)
1989
1990 do ist = 1, st%nst
1991 ncols = parse_block_cols(blk, ist-1)
1992 if (ncols /= stin%nst) then
1993 write(message(1),'(a,i6,a,i6,3a,i6,a)') "Number of columns (", ncols, ") in row ", ist, " of block ", &
1994 trim(block_name), " must equal number of states (", stin%nst, ") read from gs restart."
1995 call messages_fatal(1, namespace=namespace)
1996 end if
1997 do jst = 1, stin%nst
1998 call parse_block_cmplx(blk, ist - 1, jst - 1, rotation_matrix(jst, ist))
1999 end do
2000 end do
2001
2002 call parse_block_end(blk)
2003
2004 do iqn = st%d%kpt%start, st%d%kpt%end
2005 if (states_are_real(st)) then
2006 call states_elec_rotate(stin, mesh, real(rotation_matrix, real64) , iqn)
2007 else
2008 call states_elec_rotate(stin, mesh, rotation_matrix, iqn)
2009 end if
2010
2011 do ist = st%st_start, st%st_end
2012 call states_elec_get_state(stin, mesh, ist, iqn, psi)
2013 call states_elec_set_state(st, mesh, ist, iqn, psi)
2014 end do
2015
2016 end do
2017
2018 safe_deallocate_a(rotation_matrix)
2019 safe_deallocate_a(psi)
2020
2021 call states_elec_end(stin)
2022
2023 else
2024 call messages_variable_is_block(namespace, trim(block_name))
2025 end if
2026
2027 end if
2028
2029 pop_sub(states_elec_transform)
2030 end subroutine states_elec_transform
2031
2033
2034
2035!! Local Variables:
2036!! mode: f90
2037!! coding: utf-8
2038!! End:
Copies a vector x, to a vector y.
Definition: lalg_basic.F90:197
block signals while writing the restart files
Definition: restart.F90:298
unblock signals when writing restart is finished
Definition: restart.F90:305
This module implements batches of mesh functions.
Definition: batch.F90:112
integer, parameter, public batch_not_packed
functions are stored in CPU memory, unpacked order
Definition: batch.F90:293
integer, parameter, public batch_device_packed
functions are stored in device memory in packed order
Definition: batch.F90:293
integer, parameter, public batch_packed
functions are stored in CPU memory, in transposed (packed) order
Definition: batch.F90:293
This module handles the calculation mode.
integer, parameter, public p_strategy_max
integer, parameter, public p_strategy_domains
parallelization in domains
integer, parameter, public spinors
real(real64), parameter, public m_zero
Definition: global.F90:173
real(real64), parameter, public m_epsilon
Definition: global.F90:189
complex(real64), parameter, public m_z1
Definition: global.F90:184
subroutine, public zio_function_input(filename, namespace, space, mesh, ff, ierr, map)
Reads a mesh function from file filename, and puts it into ff. If the map argument is passed,...
System information (time, memory, sysname)
Definition: loct.F90:94
subroutine, public loct_progress_bar(a, maxcount)
A wrapper around the progress bar, such that it can be silenced without needing to dress the call wit...
Definition: loct.F90:253
logical function, public loct_dir_exists(dirname)
Definition: loct.F90:326
This module is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:94
This module defines functions over batches of mesh functions.
Definition: mesh_batch.F90:95
subroutine, public zmesh_batch_exchange_points(mesh, aa, forward_map, backward_map)
This functions exchanges points of a mesh according to a certain map. Two possible maps can be given....
subroutine, public dmesh_batch_exchange_points(mesh, aa, forward_map, backward_map)
This functions exchanges points of a mesh according to a certain map. Two possible maps can be given....
This module defines various routines, operating on mesh functions.
subroutine, public zmf_normalize(mesh, dim, psi, norm)
Normalize a mesh function psi.
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:97
subroutine, public messages_print_with_emphasis(msg, iunit, namespace)
Definition: messages.F90:878
subroutine, public messages_not_implemented(feature, namespace)
Definition: messages.F90:1048
character(len=512), private msg
Definition: messages.F90:143
subroutine, public messages_variable_is_block(namespace, name)
Definition: messages.F90:1004
subroutine, public messages_warning(no_lines, all_nodes, namespace)
Definition: messages.F90:501
subroutine, public print_date(str)
Definition: messages.F90:963
subroutine, public messages_new_line()
Definition: messages.F90:1069
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:138
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:386
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:570
This module handles the communicators for the various parallelization strategies.
Definition: multicomm.F90:124
this module contains the low-level part of the output system
Definition: output_low.F90:94
character(len=max_path_len) function, public get_filename_with_spin(output, nspin, spin_index)
Returns the filame as output, or output-spX is spin polarized.
Definition: output_low.F90:210
Some general things and nomenclature:
Definition: par_vec.F90:150
integer(int64) function, public par_vec_local2global(pv, ip)
Returns global index of local point ip.
Definition: par_vec.F90:793
logical function, public parse_is_defined(namespace, name)
Definition: parser.F90:440
subroutine, public parse_block_string(blk, l, c, res, convert_to_c)
Definition: parser.F90:795
integer function, public parse_block(namespace, name, blk, check_varinfo_)
Definition: parser.F90:600
/* To use the profiling module, call profiling_in and profiling_out with the label of the region as a...
Definition: profiling.F90:122
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
Definition: profiling.F90:672
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
Definition: profiling.F90:604
subroutine, public smear_init(this, namespace, ispin, fixed_occ, integral_occs, kpoints)
Definition: smear.F90:171
pure logical function, public states_are_complex(st)
pure logical function, public states_are_real(st)
This module handles spin dimensions of the states and the k-point distribution.
integer pure function, public states_elec_block_max(st, ib)
return index of last state in block ib
subroutine, public states_elec_end(st)
finalize the states_elec_t object
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_copy(stout, stin, exclude_wfns, exclude_eigenval, special, dest_type, copy_data)
make a (selective) copy of a states_elec_t object
subroutine, public states_elec_generate_random(st, mesh, kpoints, ist_start_, ist_end_, ikpt_start_, ikpt_end_, normalized)
randomize states
integer pure function, public states_elec_block_min(st, ib)
return index of first state in block ib
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_read_user_def_orbitals(mesh, namespace, space, st)
the routine reads formulas for user-defined wavefunctions from the input file and fills the respectiv...
subroutine, public states_elec_load_frozen(restart, space, st, mesh, ierr)
subroutine states_elec_fill_random(st, mesh, kpoints, filled)
Put random functions in orbitals that could not be read.
subroutine, public states_elec_look_and_load(restart, namespace, space, st, mesh, kpoints, fixed_occ, is_complex, packed)
subroutine states_elec_dump_adios2(restart, st, mesh, ierr)
subroutine, public states_elec_transform(st, namespace, space, restart, mesh, kpoints, prefix)
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 states_elec_dump(restart, space, st, mesh, kpoints, ierr, iter, lr, verbose)
subroutine, public states_elec_load_spin(restart, st, ierr)
returns in ierr: <0 => Fatal error, or nothing read =0 => read all wavefunctions >0 => could only rea...
subroutine, public states_elec_dump_frozen(restart, space, st, mesh, ierr)
subroutine states_elec_load_adios2(restart, st, mesh, iread, filled, restart_file_present, ierr, skip, lowest_missing)
subroutine, public states_elec_load_rho(restart, st, mesh, ierr)
subroutine, public states_elec_dump_rho(restart, st, mesh, ierr, iter)
subroutine, public states_elec_dump_spin(restart, st, ierr)
subroutine, public conv_to_c_string(str)
converts to c string
Definition: string.F90:211
type(type_t), parameter, public type_cmplx
Definition: types.F90:114
type(type_t), parameter, public type_float
Definition: types.F90:113
logical function index_is_wrong()
Describes mesh distribution to nodes.
Definition: mesh.F90:165
The states_elec_t class contains all electronic wave functions.
batches of electronic states
Definition: wfs_elec.F90:120
int true(void)