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