33 use,
intrinsic :: iso_fortran_env
82 type(restart_t),
intent(in) :: restart
83 type(namespace_t),
intent(in) :: namespace
84 class(space_t),
intent(in) :: space
85 type(states_elec_t),
target,
intent(inout) :: st
86 class(mesh_t),
intent(in) :: mesh
87 type(kpoints_t),
intent(in) :: kpoints
88 logical,
optional,
intent(in) :: is_complex
89 logical,
optional,
intent(in) :: packed
91 integer :: nkpt, dim, nst, ierr
92 real(real64),
allocatable :: new_occ(:,:)
99 message(1) =
"Unable to read states information."
103 if (st%parallel_in_states)
then
104 message(1) =
"Internal error: cannot use states_elec_look_and_load when parallel in states."
109 allocate(new_occ(1:nst, 1:st%nik))
111 new_occ(1:min(nst, st%nst),:) = st%occ(1:min(nst, st%nst),:)
112 safe_deallocate_a(st%occ)
113 call move_alloc(new_occ, st%occ)
123 safe_deallocate_a(st%node)
124 safe_allocate(st%node(1:st%nst))
127 safe_deallocate_a(st%eigenval)
128 safe_allocate(st%eigenval(1:st%nst, 1:st%nik))
129 st%eigenval = huge(st%eigenval)
131 if (
present(is_complex))
then
142 if (st%d%ispin ==
spinors)
then
143 safe_allocate(st%spin(1:3, 1:st%nst, 1:st%nik))
150 message(1) =
"Unable to read wavefunctions."
159 subroutine states_elec_dump(restart, space, st, mesh, kpoints, ierr, iter, lr, verbose)
160 type(restart_t),
intent(in) :: restart
161 class(space_t),
intent(in) :: space
162 type(states_elec_t),
target,
intent(in) :: st
163 class(mesh_t),
intent(in) :: mesh
164 type(kpoints_t),
intent(in) :: kpoints
165 integer,
intent(out) :: ierr
166 integer,
optional,
intent(in) :: iter
167 type(lr_t),
optional,
intent(in) :: lr
168 logical,
optional,
intent(in) :: verbose
170 integer :: iunit_wfns, iunit_occs, iunit_states
171 integer :: err, err2(2), ik, idir, ist, idim, itot
172 integer :: root(1:P_STRATEGY_MAX)
173 character(len=MAX_PATH_LEN) :: filename
174 character(len=300) :: lines(3)
175 logical :: lr_wfns_are_associated, should_write, verbose_
176 real(real64) :: kpoint(space%dim)
177 real(real64),
allocatable :: dpsi(:), rff_global(:)
178 complex(real64),
allocatable :: zpsi(:), zff_global(:)
186 if (restart%skip())
then
192 message(1) =
"Info: Writing states."
198 if (
present(lr))
then
199 lr_wfns_are_associated = (
allocated(lr%ddl_psi) .and.
states_are_real(st)) .or. &
201 assert(lr_wfns_are_associated)
206 iunit_states = restart%open(
'states')
207 write(lines(1),
'(a20,1i10)')
'nst= ', st%nst
208 write(lines(2),
'(a20,1i10)')
'dim= ', st%d%dim
209 write(lines(3),
'(a20,1i10)')
'nik= ', st%nik
210 call restart%write(iunit_states, lines, 3, err)
211 if (err /= 0) ierr = ierr + 1
212 call restart%close(iunit_states)
215 iunit_wfns = restart%open(
'wfns')
216 lines(1) =
'# #k-point #st #dim filename'
218 lines(2) =
'%Real_Wavefunctions'
220 lines(2) =
'%Complex_Wavefunctions'
222 call restart%write(iunit_wfns, lines, 2, err)
223 if (err /= 0) ierr = ierr + 2
226 iunit_occs = restart%open(
'occs')
227 lines(1) =
'# occupations | eigenvalue[a.u.] | Im(eigenvalue) [a.u.] | k-points | k-weights | filename | ik | ist | idim'
228 lines(2) =
'%Occupations_Eigenvalues_K-Points'
229 call restart%write(iunit_occs, lines, 2, err)
230 if (err /= 0) ierr = ierr + 4
234 safe_allocate(dpsi(1:mesh%np))
235 safe_allocate(rff_global(1:mesh%np_global))
237 safe_allocate(zpsi(1:mesh%np))
238 safe_allocate(zff_global(1:mesh%np_global))
245 kpoint(1:space%dim) = &
246 kpoints%get_point(st%d%get_kpoint_index(ik), absolute_coordinates = .
true.)
249 do idim = 1, st%d%dim
251 write(filename,
'(i10.10)') itot
253 write(lines(1),
'(i8,a,i8,a,i8,3a)') ik,
' | ', ist,
' | ', idim,
' | "', trim(filename),
'"'
254 call restart%write(iunit_wfns, lines, 1, err)
255 if (err /= 0) err2(1) = err2(1) + 1
257 write(lines(1),
'(e23.16,a,e23.16)') st%occ(ist,ik),
' | ', st%eigenval(ist, ik)
258 write(lines(1),
'(a,a,e23.16)') trim(lines(1)),
' | ',
m_zero
259 do idir = 1, space%dim
260 write(lines(1),
'(a,a,e23.16)') trim(lines(1)),
' | ', kpoint(idir)
262 write(lines(1),
'(a,a,e23.16,a,i10.10,3(a,i8))') trim(lines(1)), &
263 ' | ', st%kweights(ik),
' | ', itot,
' | ', ik,
' | ', ist,
' | ', idim
264 call restart%write(iunit_occs, lines, 1, err)
265 if (err /= 0) err2(1) = err2(1) + 1
267 should_write = st%st_start <= ist .and. ist <= st%st_end
269 if (restart%file_format_states == option__restartfileformatstates__obf)
then
270 if (should_write)
then
271 if (st%d%kpt%start <= ik .and. ik <= st%d%kpt%end)
then
273 if (.not.
present(lr))
then
275 call restart%write_mesh_function(space, filename, mesh, dpsi, err, root = root)
277 call restart%write_mesh_function(space, filename, mesh, &
278 lr%ddl_psi(:, idim, ist, ik), err, root = root)
281 if (.not.
present(lr))
then
283 call restart%write_mesh_function(space, filename, mesh, zpsi, err, root = root)
285 call restart%write_mesh_function(space, filename, mesh, &
286 lr%zdl_psi(:, idim, ist, ik), err, root = root)
293 if (err /= 0) err2(2) = err2(2) + 1
300 if (err2(1) /= 0) ierr = ierr + 8
301 if (err2(2) /= 0) ierr = ierr + 16
303 safe_deallocate_a(dpsi)
304 safe_deallocate_a(zpsi)
305 safe_deallocate_a(rff_global)
306 safe_deallocate_a(zff_global)
308 if (restart%file_format_states == option__restartfileformatstates__adios2)
then
309 if (
present(lr))
then
316 call restart%write(iunit_occs, lines, 1, err)
317 if (err /= 0) ierr = ierr + 32
318 call restart%write(iunit_wfns, lines, 1, err)
319 if (err /= 0) ierr = ierr + 64
320 if (
present(iter))
then
321 write(lines(1),
'(a,i7)')
'Iter = ', iter
322 call restart%write(iunit_wfns, lines, 1, err)
323 if (err /= 0) ierr = ierr + 128
326 call restart%close(iunit_wfns)
327 call restart%close(iunit_occs)
330 message(1) =
"Info: Finished writing states."
344 class(
mesh_t),
intent(in) :: mesh
345 integer,
intent(out) :: ierr
348 type(adios2_adios) :: adios
349 type(adios2_io) :: io
350 type(adios2_variable) :: var, var_indices
351 type(adios2_attribute) :: attribute
352 type(adios2_engine) :: engine
353 integer :: adios2_type, ik, ib, ip, adios2_mode
354 integer(int64),
allocatable :: global_indices(:)
361 call adios2_init(adios, mpi_comm_world%MPI_VAL, ierr)
363 call adios2_init(adios, ierr)
365 call check_error(.not. adios%valid .or. ierr /= adios2_error_none,
"Problem initializing ADIOS2.")
366 call adios2_declare_io(io, adios,
"writer", ierr)
367 call check_error(.not. io%valid .or. ierr /= adios2_error_none,
"Problem initializing ADIOS2.")
369 call adios2_open(engine, io, trim(restart%dir())//
"/"//
"wavefunctions.bp", adios2_mode_write, ierr)
370 call check_error(.not. engine%valid .or. ierr /= adios2_error_none,
"Problem opening ADIOS2 restart file.")
372 adios2_type = adios2_type_dp
374 adios2_type = adios2_type_complex_dp
377 call adios2_define_variable(var, io,
"wavefunctions", adios2_type, 3, &
378 [int(st%nst * st%d%dim, int64), int(mesh%np_global, int64), int(st%nik, int64)], &
379 [0_int64, 0_int64, 0_int64], &
380 [1_int64, 1_int64, 1_int64], &
381 adios2_variable_dims, ierr)
382 call check_error(.not. var%valid .or. ierr /= adios2_error_none,
"Problem creating ADIOS2 variable (wavefunctions).")
384 call adios2_define_attribute(attribute, io,
"ParDomains", mesh%mpi_grp%size, ierr)
385 call check_error(.not. attribute%valid .or. ierr /= adios2_error_none,
"Problem creating ADIOS2 attribute (ParDomains).")
387 call adios2_define_variable(var_indices, io,
"global_indices", adios2_type_integer8, 1, &
388 [int(mesh%np_global, int64)], &
389 [int(mesh%pv%xlocal-1, int64)], &
390 [int(mesh%np, int64)], &
391 adios2_variable_dims, ierr)
392 call check_error(.not. var_indices%valid .or. ierr /= adios2_error_none,
"Problem creating ADIOS2 variable (global_indices).")
394 safe_allocate(global_indices(mesh%np))
399 call adios2_begin_step(engine, ierr)
400 call adios2_put(engine, var_indices, global_indices, ierr)
402 do ik = st%d%kpt%start, st%d%kpt%end
403 do ib = st%group%block_start, st%group%block_end
405 select case (st%group%psib(ib, ik)%status())
408 call st%group%psib(ib, ik)%copy_to(psib)
412 adios2_mode = adios2_mode_sync
414 psib => st%group%psib(ib, ik)
415 adios2_mode = adios2_mode_deferred
418 call st%group%psib(ib, ik)%copy_to(psib, copy_data=.
true.)
420 call psib%do_unpack(force=.
true.)
424 adios2_mode = adios2_mode_sync
431 call adios2_set_selection(var, 3, &
432 [int(st%group%block_range(ib, 1)-1, int64), int(mesh%pv%xlocal-1, int64), int(ik-1, int64)], &
433 [int(st%group%psib(ib, ik)%nst_linear, int64), int(mesh%np, int64), 1_int64], ierr)
436 call adios2_set_memory_selection(var, 3, [0_int64, 0_int64, 0_int64], &
437 [int(
size(psib%dff_pack, 1), int64), &
438 int(
size(psib%dff_pack, 2), int64), 1_int64], ierr)
439 call adios2_put(engine, var, psib%dff_pack, adios2_mode, ierr)
441 call adios2_set_memory_selection(var, 3, [0_int64, 0_int64, 0_int64], &
442 [int(
size(psib%zff_pack, 1), int64), &
443 int(
size(psib%zff_pack, 2), int64), 1_int64], ierr)
444 call adios2_put(engine, var, psib%zff_pack, adios2_mode, ierr)
446 select case (st%group%psib(ib, ik)%status())
449 safe_deallocate_p(psib)
455 call adios2_end_step(engine, ierr)
458 safe_deallocate_a(global_indices)
461 call adios2_close(engine, ierr)
462 call check_error(ierr /= adios2_error_none,
"Problem closing ADIOS2 engine.")
463 call adios2_finalize(adios, ierr)
464 call check_error(ierr /= adios2_error_none,
"Problem finalizing ADIOS2.")
479 subroutine states_elec_load(restart, namespace, space, st, mesh, kpoints, ierr, iter, lr, lowest_missing, label, verbose, skip)
482 class(
space_t),
intent(in) :: space
484 class(
mesh_t),
intent(in) :: mesh
486 integer,
intent(out) :: ierr
487 integer,
optional,
intent(out) :: iter
488 type(
lr_t),
optional,
intent(inout) :: lr
489 integer,
optional,
intent(out) :: lowest_missing(:, :)
490 character(len=*),
optional,
intent(in) :: label
491 logical,
optional,
intent(in) :: verbose
492 logical,
optional,
intent(in) :: skip(:)
494 integer :: states_elec_file, wfns_file, occ_file, err, ik, ist, idir, idim
495 integer :: idone, iread, ntodo
496 character(len=12) :: filename
497 character(len=1) :: char
498 logical,
allocatable :: filled(:, :, :)
499 character(len=256) :: lines(3), label_
500 character(len=50) :: str
502 real(real64) :: my_occ, imev, my_kweight
503 logical :: read_occ, lr_allocated, verbose_
504 logical :: integral_occs
505 real(real64),
allocatable :: dpsi(:)
506 complex(real64),
allocatable :: zpsi(:), zpsil(:)
507 character(len=256),
allocatable :: restart_file(:, :, :)
508 logical,
allocatable :: restart_file_present(:, :, :)
509 real(real64) :: kpoint(space%dim), read_kpoint(space%dim)
512 integer,
allocatable :: lowest_missing_tmp(:, :)
519 if (
present(lowest_missing)) lowest_missing = 1
520 if (
present(iter)) iter = 0
522 if (
present(skip))
then
523 assert(ubound(skip, dim = 1) == st%nst)
526 if (restart%skip())
then
536 if (
present(label))
then
539 if (
present(lr))
then
540 label_ =
" for linear response"
546 message(1) =
'Info: Reading states'
547 if (len(trim(label_)) > 0)
then
553 if (.not.
present(lr))
then
554 st%fromScratch = .false.
560 integral_occs = .
true.
561 if (st%restart_fixed_occ)
then
563 st%fixed_occ = .
true.
565 read_occ = .not. st%fixed_occ
568 if (.not.
present(lr))
then
569 st%eigenval(:, :) =
m_zero
573 if (.not.
present(lr) .and. read_occ)
then
579 if (
present(lr))
then
580 lr_allocated = (
allocated(lr%ddl_psi) .and.
states_are_real(st)) .or. &
585 states_elec_file = restart%open(
'states')
590 call restart%read(states_elec_file, lines, 3, err)
594 read(lines(2), *) str, idim
595 read(lines(3), *) str, ik
596 if (idim == 2 .and. st%d%dim == 1)
then
597 write(
message(1),
'(a)')
'Incompatible restart information: saved calculation is spinors, this one is not.'
601 if (idim == 1 .and. st%d%dim == 2)
then
602 write(
message(1),
'(a)')
'Incompatible restart information: this calculation is spinors, saved one is not.'
606 if (ik < st%nik)
then
607 write(
message(1),
'(a)')
'Incompatible restart information: not enough k-points.'
608 write(
message(2),
'(2(a,i6))')
'Expected ', st%nik,
' > Read ', ik
613 call restart%close(states_elec_file)
617 wfns_file = restart%open(
'wfns')
618 occ_file = restart%open(
'occs')
619 call restart%read(wfns_file, lines, 2, err)
623 read(lines(2),
'(a)') str
624 if (str(2:8) ==
'Complex')
then
625 message(1) =
"Cannot read real states from complex wavefunctions."
628 else if (str(2:5) /=
'Real')
then
629 message(1) =
"Restart file 'wfns' does not specify real/complex; cannot check compatibility."
636 call restart%read(occ_file, lines, 2, err)
637 if (err /= 0) ierr = ierr - 2**7
642 call restart%close(wfns_file)
643 call restart%close(occ_file)
650 safe_allocate(dpsi(1:mesh%np))
652 safe_allocate(zpsi(1:mesh%np))
655 safe_allocate(restart_file(1:st%d%dim, st%st_start:st%st_end, 1:st%nik))
656 safe_allocate(restart_file_present(1:st%d%dim, st%st_start:st%st_end, 1:st%nik))
657 restart_file_present = .false.
663 call restart%read(wfns_file, lines, 1, err)
665 read(lines(1),
'(a)') char
666 if (char ==
'%')
then
670 read(lines(1), *) ik, char, ist, char, idim, char, filename
675 call restart%read(occ_file, lines, 1, err)
679 if (ist >= st%st_start .and. ist <= st%st_end .and. &
680 ik >= st%d%kpt%start .and. ik <= st%d%kpt%end)
then
682 restart_file(idim, ist, ik) = trim(filename)
683 restart_file_present(idim, ist, ik) = .
true.
686 call restart%read(occ_file, lines, 1, err)
687 if (.not.
present(lr))
then
691 read(lines(1), *) my_occ, char, st%eigenval(ist, ik), char, imev, char, &
692 (read_kpoint(idir), char, idir = 1, space%dim), my_kweight
696 if (ist >= st%st_start .and. ist <= st%st_end .and. &
697 ik >= st%d%kpt%start .and. ik <= st%d%kpt%end)
then
698 restart_file_present(idim, ist, ik) = .false.
703 kpoint(1:space%dim) = &
704 kpoints%get_point(st%d%get_kpoint_index(ik), absolute_coordinates = .
true.)
706 if (any(abs(kpoint(1:space%dim) - read_kpoint(1:space%dim)) > 1e-12_real64))
then
709 write(
message(1),
'(a,i6)')
'Incompatible restart information: k-point mismatch for ik ', ik
710 write(
message(2),
'(a,99f18.12)')
' Expected : ', kpoint(1:space%dim)
711 write(
message(3),
'(a,99f18.12)')
' Read : ', read_kpoint(1:space%dim)
714 if (ist >= st%st_start .and. ist <= st%st_end .and. &
715 ik >= st%d%kpt%start .and. ik <= st%d%kpt%end)
then
716 restart_file_present(idim, ist, ik) = .false.
721 st%occ(ist, ik) = my_occ
722 integral_occs = integral_occs .and. &
723 abs((st%occ(ist, ik) - st%smear%el_per_state) * st%occ(ist, ik)) <=
m_epsilon
728 if (
present(iter))
then
729 call restart%read(wfns_file, lines, 1, err)
733 read(lines(1), *) filename, filename, iter
737 call restart%close(wfns_file)
738 call restart%close(occ_file)
744 safe_allocate(filled(1:st%d%dim, st%st_start:st%st_end, st%d%kpt%start:st%d%kpt%end))
747 if (
present(lowest_missing)) lowest_missing = st%nst + 1
750 if (
mpi_world%is_root() .and. verbose_)
then
752 ntodo = st%lnst*st%d%kpt%nlocal*st%d%dim
755 if (restart%file_format_states == option__restartfileformatstates__obf)
then
756 do ik = st%d%kpt%start, st%d%kpt%end
757 do ist = st%st_start, st%st_end
758 if (
present(skip))
then
762 do idim = 1, st%d%dim
764 if (.not. restart_file_present(idim, ist, ik))
then
765 if (
present(lowest_missing))
then
766 lowest_missing(idim, ik) = min(lowest_missing(idim, ik), ist)
772 call restart%read_mesh_function(space, restart_file(idim, ist, ik), mesh, dpsi, err)
773 if (.not.
present(lr))
then
776 call lalg_copy(mesh%np, dpsi, lr%ddl_psi(:, idim, ist, ik))
779 call restart%read_mesh_function(space, restart_file(idim, ist, ik), mesh, zpsi, err)
780 if (.not.
present(lr))
then
783 call lalg_copy(mesh%np, zpsi, lr%zdl_psi(:, idim, ist, ik))
788 filled(idim, ist, ik) = .
true.
790 else if (
present(lowest_missing))
then
791 lowest_missing(idim, ik) = min(lowest_missing(idim, ik), ist)
794 if (
mpi_world%is_root() .and. verbose_)
then
802 else if (restart%file_format_states == option__restartfileformatstates__adios2)
then
803 if (
present(lr))
then
806 if (
present(skip))
then
809 if (restart%has_map())
then
815 safe_deallocate_a(dpsi)
816 safe_deallocate_a(zpsi)
817 safe_deallocate_a(zpsil)
818 safe_deallocate_a(restart_file)
819 safe_deallocate_a(restart_file_present)
821 if (
mpi_world%is_root() .and. verbose_)
then
825 if (st%parallel_in_states .or. st%d%kpt%parallel)
then
827 call st%st_kpt_mpi_grp%allreduce(iread_tmp, iread, 1, mpi_integer, mpi_sum)
830 if (st%d%kpt%parallel)
then
832 if (
present(lowest_missing))
then
833 safe_allocate(lowest_missing_tmp(1:st%d%dim, 1:st%nik))
834 lowest_missing_tmp = lowest_missing
835 call st%d%kpt%mpi_grp%allreduce(lowest_missing_tmp(1,1), lowest_missing(1,1), st%d%dim*st%nik, &
836 mpi_integer, mpi_min)
837 safe_deallocate_a(lowest_missing_tmp)
841 if (st%restart_fixed_occ .and. iread == st%nst * st%nik * st%d%dim)
then
844 call smear_init(st%smear, namespace, st%d%ispin, fixed_occ = .
true., integral_occs = integral_occs, kpoints = kpoints)
847 if (.not.
present(lr) .and. .not.
present(skip))
call fill_random()
850 safe_deallocate_a(filled)
852 if (ierr == 0 .and. iread /= st%nst * st%nik * st%d%dim)
then
860 if (.not.
present(lr))
then
861 write(str,
'(a,i5)')
'Reading states.'
863 write(str,
'(a,i5)')
'Reading states information for linear response.'
866 if (.not.
present(skip))
then
867 write(
message(1),
'(a,i6,a,i6,a)')
'Only ', iread,
' files out of ', &
868 st%nst * st%nik * st%d%dim,
' could be read.'
870 write(
message(1),
'(a,i6,a,i6,a)')
'Only ', iread,
' files out of ', &
871 st%nst * st%nik * st%d%dim,
' were loaded.'
878 message(1) =
'Info: States reading done.'
890 do ik = st%d%kpt%start, st%d%kpt%end
892 do ist = st%st_start, st%st_end
893 do idim = 1, st%d%dim
894 if (filled(idim, ist, ik)) cycle
906 logical function index_is_wrong() !< .
true. if the index (idim, ist, ik) is not present in st structure...
909 if (idim > st%d%dim .or. idim < 1 .or. &
910 ist > st%nst .or. ist < 1 .or. &
911 ik > st%nik .or. ik < 1)
then
926 class(
mesh_t),
intent(in) :: mesh
927 integer,
intent(out) :: iread
928 logical,
intent(out) :: filled(1:, st%st_start:, st%d%kpt%start:)
929 integer,
intent(out) :: ierr
932 type(adios2_adios) :: adios
933 type(adios2_io) :: io
934 type(adios2_variable) :: var, var_indices
935 type(adios2_attribute) :: attribute
936 type(adios2_engine) :: engine
937 integer :: type_file, type_requested, ib, ik, ist, ndims, ip, pardomains, adios2_mode
938 integer(int64),
allocatable :: var_shape(:)
939 real(real64),
allocatable :: dff_pack(:, :)
940 integer(int64),
allocatable :: global_indices(:)
947 call adios2_init(adios, mpi_comm_world%MPI_VAL, ierr)
949 call adios2_init(adios, ierr)
951 call check_error(.not. adios%valid .or. ierr /= adios2_error_none,
"Problem initializing ADIOS2.")
952 call adios2_declare_io(io, adios,
"reader", ierr)
953 call check_error(.not. io%valid .or. ierr /= adios2_error_none,
"Problem initializing ADIOS2.")
955 call adios2_open(engine, io, trim(restart%dir())//
"/"//
"wavefunctions.bp", adios2_mode_read, ierr)
956 call check_error(.not. engine%valid .or. ierr /= adios2_error_none,
"Problem opening ADIOS2 restart file.")
958 call adios2_begin_step(engine, ierr)
960 call adios2_inquire_attribute(attribute, io,
"ParDomains", ierr)
961 call check_error(.not. attribute%valid .or. ierr /= adios2_error_none,
"Problem inquiring ADIOS2 attribute.")
962 call adios2_attribute_data(pardomains, attribute, ierr)
964 type_requested = adios2_type_dp
966 type_requested = adios2_type_complex_dp
968 call adios2_inquire_variable(var, io,
'wavefunctions', ierr)
969 call check_error(.not. var%valid .or. ierr /= adios2_error_none,
"Problem loading ADIOS2 variable (wavefunctions).")
971 call adios2_variable_type(type_file, var, ierr)
972 call adios2_variable_ndims(ndims, var, ierr)
973 call adios2_variable_shape(var_shape, ndims, var, ierr)
975 if (var_shape(1) < int(st%nst * st%d%dim, int64))
then
976 message(1) =
"Error: more states requested than available in the restart data. Not supported with ADIOS2 format."
979 if (var_shape(2) /= int(mesh%np_global, int64))
then
981 message(1) =
"Error: trying to restart with a different number of grid points. Not supported with ADIOS2 format."
984 if (var_shape(3) /= int(st%nik, int64))
then
985 message(1) =
"Error: trying to restart with a different number of k points. Not supported with ADIOS2 format."
988 do ik = st%d%kpt%start, st%d%kpt%end
989 do ib = st%group%block_start, st%group%block_end
993 select case (st%group%psib(ib, ik)%status())
996 call st%group%psib(ib, ik)%copy_to(psib, copy_data=.false.)
998 adios2_mode = adios2_mode_sync
1000 psib => st%group%psib(ib, ik)
1001 adios2_mode = adios2_mode_deferred
1004 call st%group%psib(ib, ik)%copy_to(psib)
1005 call psib%do_unpack(force=.
true., copy=.false.)
1008 adios2_mode = adios2_mode_sync
1015 call adios2_set_selection(var, 3, &
1016 [int(st%group%block_range(ib, 1)-1, int64), int(mesh%pv%xlocal-1, int64), int(ik-1, int64)], &
1017 [int(st%group%psib(ib, ik)%nst_linear, int64), int(mesh%np, int64), 1_int64], ierr)
1019 if (type_requested == adios2_type_dp .and. type_file == adios2_type_dp)
then
1021 call adios2_set_memory_selection(var, 3, [0_int64, 0_int64, 0_int64], &
1022 [int(
size(psib%dff_pack, 1), int64), &
1023 int(
size(psib%dff_pack, 2), int64), 1_int64], ierr)
1025 call adios2_get(engine, var, psib%dff_pack, adios2_mode, ierr)
1026 else if (type_requested == adios2_type_complex_dp .and. type_file == adios2_type_complex_dp)
then
1028 call adios2_set_memory_selection(var, 3, [0_int64, 0_int64, 0_int64], &
1029 [int(
size(psib%zff_pack, 1), int64), &
1030 int(
size(psib%zff_pack, 2), int64), 1_int64], ierr)
1032 call adios2_get(engine, var, psib%zff_pack, adios2_mode, ierr)
1033 else if (type_requested == adios2_type_complex_dp .and. type_file == adios2_type_dp)
then
1035 safe_allocate(dff_pack(
size(psib%zff_pack, 1),
size(psib%zff_pack, 2)))
1037 call adios2_set_selection(var, 3, &
1038 [int(st%group%block_range(ib, 1)-1, int64), int(mesh%pv%xlocal-1, int64), int(ik-1, int64)], &
1039 [int(psib%nst_linear, int64), int(mesh%np, int64), 1_int64], ierr)
1041 call adios2_set_memory_selection(var, 3, [0_int64, 0_int64, 0_int64], &
1042 [int(
size(psib%zff_pack, 1), int64), &
1043 int(
size(psib%zff_pack, 2), int64), 1_int64], ierr)
1046 call adios2_get(engine, var, dff_pack, adios2_mode_sync, ierr)
1050 do ist = 1, psib%nst_linear
1051 psib%zff_pack(ist, ip) = cmplx(dff_pack(ist, ip),
m_zero, real64)
1054 safe_deallocate_a(dff_pack)
1056 message(1) =
"Error: requested to read complex states for restarting, but calculation has real states."
1060 iread = iread + st%group%psib(ib, ik)%nst_linear
1061 do ist = 1, st%group%psib(ib, ik)%nst_linear
1062 filled(st%group%psib(ib, ik)%linear_to_idim(ist), st%group%psib(ib, ik)%linear_to_ist(ist), ik) = .
true.
1065 select case (st%group%psib(ib, ik)%status())
1067 call psib%do_unpack()
1068 call psib%copy_data_to(mesh%np, st%group%psib(ib, ik))
1070 safe_deallocate_p(psib)
1075 call psib%copy_data_to(mesh%np, st%group%psib(ib, ik))
1077 safe_deallocate_p(psib)
1082 if (pardomains /= mesh%mpi_grp%size)
then
1083 call adios2_inquire_variable(var_indices, io,
'global_indices', ierr)
1084 call check_error(.not. var_indices%valid .or. ierr /= adios2_error_none, &
1085 "Problem loading ADIOS2 variable (global_indices).")
1086 safe_allocate(global_indices(mesh%np))
1087 call adios2_set_selection(var_indices, 1, &
1088 [int(mesh%pv%xlocal-1, int64)], &
1089 [int(mesh%np, int64)], ierr)
1090 call adios2_get(engine, var_indices, global_indices, ierr)
1092 call adios2_end_step(engine, ierr)
1093 if (pardomains /= mesh%mpi_grp%size)
then
1094 do ik = st%d%kpt%start, st%d%kpt%end
1095 do ib = st%group%block_start, st%group%block_end
1103 safe_deallocate_a(global_indices)
1107 call adios2_close(engine, ierr)
1108 call check_error(ierr /= adios2_error_none,
"Problem closing ADIOS2 engine.")
1109 call adios2_finalize(adios, ierr)
1110 call check_error(ierr /= adios2_error_none,
"Problem finalizing ADIOS2.")
1121 subroutine check_error(condition, error_message)
1122 logical,
intent(in) :: condition
1123 character(len=*),
intent(in) :: error_message
1128 end subroutine check_error
1133 class(
space_t),
intent(in) :: space
1135 class(
mesh_t),
intent(in) :: mesh
1136 integer,
intent(out) :: ierr
1137 integer,
optional,
intent(in) :: iter
1139 integer :: iunit, isp, err, err2(2)
1140 character(len=MAX_PATH_LEN) :: filename
1141 character(len=300) :: lines(2)
1147 if (restart%skip())
then
1152 message(1) =
"Debug: Writing density restart."
1158 iunit = restart%open(
'density')
1159 lines(1) =
'# #spin #nspin filename'
1160 lines(2) =
'%densities'
1161 call restart%write(iunit, lines, 2, err)
1162 if (err /= 0) ierr = ierr + 1
1165 do isp = 1, st%d%nspin
1167 write(lines(1),
'(i8,a,i8,a)') isp,
' | ', st%d%nspin,
' | "'//trim(adjustl(filename))//
'"'
1168 call restart%write(iunit, lines, 1, err)
1169 if (err /= 0) err2(1) = err2(1) + 1
1171 call restart%write_mesh_function(space, filename, mesh, st%rho(:,isp), err)
1172 if (err /= 0) err2(2) = err2(2) + 1
1175 if (err2(1) /= 0) ierr = ierr + 2
1176 if (err2(2) /= 0) ierr = ierr + 4
1179 call restart%write(iunit, lines, 1, err)
1180 if (err /= 0) ierr = ierr + 8
1181 if (
present(iter))
then
1182 write(lines(1),
'(a,i7)')
'Iter = ', iter
1183 call restart%write(iunit, lines, 1, err)
1184 if (err /= 0) ierr = ierr + 16
1186 call restart%close(iunit)
1190 message(1) =
"Debug: Writing density restart done."
1200 class(
space_t),
intent(in) :: space
1202 class(
mesh_t),
intent(in) :: mesh
1203 integer,
intent(out) :: ierr
1205 integer :: err, err2, isp
1206 character(len=MAX_PATH_LEN) :: filename
1212 if (restart%skip())
then
1218 message(1) =
"Debug: Reading density restart."
1229 do isp = 1, st%d%nspin
1234 call restart%read_mesh_function(space, filename, mesh, st%rho(:,isp), err)
1235 if (err /= 0) err2 = err2 + 1
1238 if (err2 /= 0) ierr = ierr + 1
1240 message(1) =
"Debug: Reading density restart done."
1248 class(
space_t),
intent(in) :: space
1250 class(
mesh_t),
intent(in) :: mesh
1251 integer,
intent(out) :: ierr
1253 integer :: isp, err, err2(2), idir
1254 character(len=MAX_PATH_LEN) :: filename
1260 assert(
allocated(st%frozen_rho))
1262 if (restart%skip())
then
1267 message(1) =
"Debug: Writing frozen densities restart."
1273 do isp = 1, st%d%nspin
1276 call restart%write_mesh_function(space, filename, mesh, st%frozen_rho(:,isp), err)
1277 if (err /= 0) err2(2) = err2(2) + 1
1279 if (
allocated(st%frozen_tau))
then
1281 call restart%write_mesh_function(space, filename, mesh, st%frozen_tau(:,isp), err)
1282 if (err /= 0) err2 = err2 + 1
1285 if (
allocated(st%frozen_gdens))
then
1286 do idir = 1, space%dim
1287 if (st%d%nspin == 1)
then
1288 write(filename, fmt=
'(a,i1)')
'frozen_gdens-dir', idir
1290 write(filename, fmt=
'(a,i1,a,i1)')
'frozen_tau-dir', idir,
'-', isp
1292 call restart%write_mesh_function(space, filename, mesh, st%frozen_gdens(:,idir,isp), err)
1293 if (err /= 0) err2 = err2 + 1
1297 if (
allocated(st%frozen_ldens))
then
1299 call restart%write_mesh_function(space, filename, mesh, st%frozen_ldens(:,isp), err)
1300 if (err /= 0) err2 = err2 + 1
1304 if (err2(1) /= 0) ierr = ierr + 2
1305 if (err2(2) /= 0) ierr = ierr + 4
1309 message(1) =
"Debug: Writing frozen densities restart done."
1319 class(
space_t),
intent(in) :: space
1321 class(
mesh_t),
intent(in) :: mesh
1322 integer,
intent(out) :: ierr
1324 integer :: err, err2, isp, idir
1325 character(len=MAX_PATH_LEN) :: filename
1329 assert(
allocated(st%frozen_rho))
1333 if (restart%skip())
then
1339 message(1) =
"Debug: Reading densities restart."
1343 do isp = 1, st%d%nspin
1345 call restart%read_mesh_function(space, filename, mesh, st%frozen_rho(:,isp), err)
1346 if (err /= 0) err2 = err2 + 1
1348 if (
allocated(st%frozen_tau))
then
1350 call restart%read_mesh_function(space, filename, mesh, st%frozen_tau(:,isp), err)
1351 if (err /= 0) err2 = err2 + 1
1354 if (
allocated(st%frozen_gdens))
then
1355 do idir = 1, space%dim
1356 if (st%d%nspin == 1)
then
1357 write(filename, fmt=
'(a,i1)')
'frozen_gdens-dir', idir
1359 write(filename, fmt=
'(a,i1,a,i1)')
'frozen_tau-dir', idir,
'-', isp
1361 call restart%read_mesh_function(space, filename, mesh, st%frozen_gdens(:,idir,isp), err)
1362 if (err /= 0) err2 = err2 + 1
1366 if (
allocated(st%frozen_ldens))
then
1368 call restart%read_mesh_function(space, filename, mesh, st%frozen_ldens(:,isp), err)
1369 if (err /= 0) err2 = err2 + 1
1373 if (err2 /= 0) ierr = ierr + 1
1375 message(1) =
"Debug: Reading frozen densities restart done."
1386 class(
mesh_t),
intent(in) :: mesh
1388 class(
space_t),
intent(in) :: space
1392 integer :: ip, id, is, ik, nstates, state_from, ierr, ncols
1393 integer :: ib, idim, inst, inik, normalize
1394 real(real64) :: xx(space%dim), rr, psi_re, psi_im
1395 character(len=150) :: filename
1396 complex(real64),
allocatable :: zpsi(:, :)
1398 integer,
parameter :: &
1399 state_from_formula = 1, &
1400 state_from_file = -10010, &
1401 normalize_yes = 1, &
1484 if (
parse_block(namespace,
'UserDefinedStates', blk) == 0)
then
1491 safe_allocate(zpsi(1:mesh%np, 1:st%d%dim))
1497 if (ncols < 5 .or. ncols > 6)
then
1498 message(1) =
'Each line in the UserDefinedStates block must have'
1499 message(2) =
'five or six columns.'
1515 if (.not.(id == idim .and. is == inst .and. ik == inik )) cycle
1521 select case (state_from)
1522 case (state_from_formula)
1525 blk, ib - 1, 4, st%user_def_states(id, is, ik))
1527 write(
message(1),
'(a,3i5)')
'Substituting state of orbital with k, ist, dim = ', ik, is, id
1528 write(
message(2),
'(2a)')
' with the expression:'
1529 write(
message(3),
'(2a)')
' ',trim(st%user_def_states(id, is, ik))
1535 case (state_from_file)
1541 write(
message(1),
'(a,3i5)')
'Substituting state of orbital with k, ist, dim = ', ik, is, id
1542 write(
message(2),
'(2a)')
' with data from file:'
1543 write(
message(3),
'(2a)')
' ',trim(filename)
1549 if (.not.(st%st_start <= is .and. st%st_end >= is &
1550 .and. st%d%kpt%start <= ik .and. st%d%kpt%end >= ik)) cycle
1552 select case (state_from)
1554 case (state_from_formula)
1563 zpsi(ip, 1) = cmplx(psi_re, psi_im, real64)
1566 case (state_from_file)
1570 message(1) =
'Could not read the file!'
1571 write(
message(2),
'(a,i1)')
'Error code: ', ierr
1576 message(1) =
'Wrong entry in UserDefinedStates, column 4.'
1577 message(2) =
'You may state "formula" or "file" here.'
1587 select case (normalize)
1590 case (normalize_yes)
1591 assert(st%d%dim == 1)
1595 message(1) =
'The sixth column in UserDefinedStates may either be'
1596 message(2) =
'"normalize_yes" or "normalize_no"'
1606 safe_deallocate_a(zpsi)
1624 integer,
intent(out) :: ierr
1626 integer :: iunit_spin
1627 integer :: err, err2(2), ik, ist
1628 character(len=300) :: lines(3)
1634 if (restart%skip())
then
1643 iunit_spin = restart%open(
'spin')
1644 lines(1) =
'# #k-point #st #spin(x) spin(y) spin(z)'
1645 call restart%write(iunit_spin, lines, 1, err)
1646 if (err /= 0) ierr = ierr + 1
1651 write(lines(1),
'(i8,a,i8,3(a,f18.12))') ik,
' | ', ist,
' | ', &
1652 st%spin(1,ist,ik),
' | ', st%spin(2,ist,ik),
' | ', st%spin(3,ist,ik)
1653 call restart%write(iunit_spin, lines, 1, err)
1654 if (err /= 0) err2(1) = err2(1) + 1
1658 call restart%write(iunit_spin, lines, 1, err)
1659 if (err2(1) /= 0) ierr = ierr + 8
1660 if (err2(2) /= 0) ierr = ierr + 16
1662 call restart%close(iunit_spin)
1681 integer,
intent(out) :: ierr
1683 integer :: spin_file, err, ik, ist
1684 character(len=256) :: lines(3)
1685 real(real64) :: spin(3)
1686 character(len=1) :: char
1693 if (restart%skip())
then
1702 spin_file = restart%open(
'spin')
1704 call restart%read(spin_file, lines, 1, err)
1705 if (err /= 0) ierr = ierr - 2**7
1710 call restart%close(spin_file)
1720 call restart%read(spin_file, lines, 1, err)
1721 read(lines(1),
'(a)') char
1722 if (char ==
'%')
then
1726 read(lines(1), *) ik, char, ist, char, spin(1), char, spin(2), char, spin(3)
1731 st%spin(1:3, ist, ik) = spin(1:3)
1734 call restart%close(spin_file)
1744 class(
space_t),
intent(in) :: space
1745 type(
restart_t),
intent(inout) :: restart
1746 class(
mesh_t),
intent(in) :: mesh
1748 character(len=*),
optional,
intent(in) :: prefix
1752 complex(real64),
allocatable :: rotation_matrix(:,:), psi(:, :)
1753 integer :: ist, jst, ncols, iqn
1754 character(len=256) :: block_name
1786 if (
parse_block(namespace, trim(block_name), blk) == 0)
then
1787 if (st%parallel_in_states)
then
1791 message(1) =
"Number of rows in block " // trim(block_name) //
" must equal number of states in this calculation."
1798 safe_allocate(rotation_matrix(1:stin%nst, 1:stin%nst))
1799 safe_allocate(psi(1:mesh%np, 1:st%d%dim))
1805 if (ncols /= stin%nst)
then
1806 write(
message(1),
'(a,i6,a,i6,3a,i6,a)')
"Number of columns (", ncols,
") in row ", ist,
" of block ", &
1807 trim(block_name),
" must equal number of states (", stin%nst,
") read from gs restart."
1810 do jst = 1, stin%nst
1817 do iqn = st%d%kpt%start, st%d%kpt%end
1824 do ist = st%st_start, st%st_end
1831 safe_deallocate_a(rotation_matrix)
1832 safe_deallocate_a(psi)
ssize_t read(int __fd, void *__buf, size_t __nbytes) __attribute__((__access__(__write_only__
Copies a vector x, to a vector y.
block signals while writing the restart files
unblock signals when writing restart is finished
This module implements batches of mesh functions.
integer, parameter, public batch_not_packed
functions are stored in CPU memory, unpacked order
integer, parameter, public batch_device_packed
functions are stored in device memory in packed order
integer, parameter, public batch_packed
functions are stored in CPU memory, in transposed (packed) order
This module handles the calculation mode.
integer, parameter, public p_strategy_domains
parallelization in domains
integer, parameter, public spinors
real(real64), parameter, public m_zero
real(real64), parameter, public m_epsilon
complex(real64), parameter, public m_z1
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,...
This module is intended to contain "only mathematical" functions and procedures.
This module defines functions over batches of mesh functions.
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.
subroutine, public messages_print_with_emphasis(msg, iunit, namespace)
subroutine, public messages_not_implemented(feature, namespace)
character(len=512), private msg
subroutine, public messages_variable_is_block(namespace, name)
subroutine, public messages_warning(no_lines, all_nodes, namespace)
subroutine, public print_date(str)
subroutine, public messages_new_line()
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
type(mpi_grp_t), public mpi_world
This module handles the communicators for the various parallelization strategies.
this module contains the low-level part of the output system
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.
Some general things and nomenclature:
integer(int64) function, public par_vec_local2global(pv, ip)
Returns global index of local point ip.
logical function, public parse_is_defined(namespace, name)
integer function, public parse_block(namespace, name, blk, check_varinfo_)
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
subroutine, public smear_init(this, namespace, ispin, fixed_occ, integral_occs, kpoints)
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.
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)
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
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, public states_elec_look_and_load(restart, namespace, space, st, mesh, kpoints, 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_dump(restart, space, st, mesh, kpoints, ierr, iter, lr, verbose)
subroutine, public states_elec_load(restart, namespace, space, st, mesh, kpoints, ierr, iter, lr, lowest_missing, label, verbose, skip)
returns in ierr: <0 => Fatal error, or nothing read =0 => read all wavefunctions >0 => could only rea...
subroutine, public 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, public states_elec_load_rho(restart, space, st, mesh, ierr)
subroutine, public states_elec_dump_rho(restart, space, st, mesh, ierr, iter)
subroutine states_elec_load_adios2(restart, st, mesh, iread, filled, ierr)
subroutine, public states_elec_dump_spin(restart, st, ierr)
subroutine, public conv_to_c_string(str)
converts to c string
type(type_t), public type_float
type(type_t), public type_cmplx
logical function index_is_wrong()
Describes mesh distribution to nodes.
The states_elec_t class contains all electronic wave functions.
batches of electronic states