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
25 use debug_oct_m
27 use global_oct_m
29 use, intrinsic :: iso_fortran_env
33 use loct_oct_m
34 use math_oct_m
35 use mesh_oct_m
38 use mpi_oct_m
42 use parser_oct_m
45 use smear_oct_m
46 use space_oct_m
51 use string_oct_m
52 use types_oct_m
53
54 implicit none
55
56
57 private
58 public :: &
70
71contains
72
73 ! ---------------------------------------------------------
74 subroutine states_elec_look_and_load(restart, namespace, space, st, mesh, kpoints, is_complex, packed)
75 type(restart_t), intent(in) :: restart
76 type(namespace_t), intent(in) :: namespace
77 class(space_t), intent(in) :: space
78 type(states_elec_t), target, intent(inout) :: st
79 class(mesh_t), intent(in) :: mesh
80 type(kpoints_t), intent(in) :: kpoints
81 logical, optional, intent(in) :: is_complex
82 logical, optional, intent(in) :: packed
83
84 integer :: nkpt, dim, nst, ierr
85 real(real64), allocatable :: new_occ(:,:)
86
88
89 !check how many wfs we have
90 call states_elec_look(restart, nkpt, dim, nst, ierr)
91 if (ierr /= 0) then
92 message(1) = "Unable to read states information."
93 call messages_fatal(1, namespace=namespace)
94 end if
95
96 if (st%parallel_in_states) then
97 message(1) = "Internal error: cannot use states_elec_look_and_load when parallel in states."
98 call messages_fatal(1, namespace=namespace)
99 end if
100
101 ! Resize st%occ, retaining information there
102 allocate(new_occ(1:nst, 1:st%nik))
103 new_occ(:,:) = m_zero
104 new_occ(1:min(nst, st%nst),:) = st%occ(1:min(nst, st%nst),:)
105 safe_deallocate_a(st%occ)
106 call move_alloc(new_occ, st%occ)
107
108 ! FIXME: This wrong, one cannot just change the number of states
109 ! without updating the internal structures, in the case of parallel in states.
110 ! I think it is right now without state parallelism.
111 st%nst = nst
112 st%st_start = 1
113 st%st_end = nst
114 st%lnst = nst
115
116 safe_deallocate_a(st%node)
117 safe_allocate(st%node(1:st%nst))
118 st%node(:) = 0
119
120 safe_deallocate_a(st%eigenval)
121 safe_allocate(st%eigenval(1:st%nst, 1:st%nik))
122 st%eigenval = huge(st%eigenval)
123
124 if (present(is_complex)) then
125 if (is_complex) then
126 call states_elec_allocate_wfns(st, mesh, type_cmplx, packed=optional_default(packed, .false.))
127 else
128 call states_elec_allocate_wfns(st, mesh, type_float, packed=optional_default(packed, .false.))
129 end if
130 else
131 ! allow states_elec_allocate_wfns to decide for itself whether complex or real needed
132 call states_elec_allocate_wfns(st, mesh, packed=optional_default(packed, .false.))
133 end if
134
135 if (st%d%ispin == spinors) then
136 safe_allocate(st%spin(1:3, 1:st%nst, 1:st%nik))
137 st%spin = m_zero
138 end if
139
140 ! load wavefunctions
141 call states_elec_load(restart, namespace, space, st, mesh, kpoints, ierr)
142 if (ierr /= 0) then
143 message(1) = "Unable to read wavefunctions."
144 call messages_fatal(1, namespace=namespace)
145 end if
146
148 end subroutine states_elec_look_and_load
149
150
151 ! ---------------------------------------------------------
152 subroutine states_elec_dump(restart, space, st, mesh, kpoints, ierr, iter, lr, st_start_writing, verbose)
153 type(restart_t), intent(in) :: restart
154 class(space_t), intent(in) :: space
155 type(states_elec_t), intent(in) :: st
156 class(mesh_t), intent(in) :: mesh
157 type(kpoints_t), intent(in) :: kpoints
158 integer, intent(out) :: ierr
159 integer, optional, intent(in) :: iter
161 type(lr_t), optional, intent(in) :: lr
162 integer, optional, intent(in) :: st_start_writing
163 logical, optional, intent(in) :: verbose
164
165 integer :: iunit_wfns, iunit_occs, iunit_states
166 integer :: err, err2(2), ik, idir, ist, idim, itot
167 integer :: root(1:p_strategy_max)
168 character(len=MAX_PATH_LEN) :: filename
169 character(len=300) :: lines(3)
170 logical :: lr_wfns_are_associated, should_write, verbose_
171 real(real64) :: kpoint(space%dim)
172 real(real64), allocatable :: dpsi(:), rff_global(:)
173 complex(real64), allocatable :: zpsi(:), zff_global(:)
174
175 push_sub(states_elec_dump)
176
177 verbose_ = optional_default(verbose, .true.)
178
179 ierr = 0
180
181 if (restart_skip(restart)) then
182 pop_sub(states_elec_dump)
183 return
184 end if
185
186 if (verbose_) then
187 message(1) = "Info: Writing states."
188 call print_date(trim(message(1))//' ')
189 end if
190
191 call profiling_in("RESTART_WRITE")
192
193 if (present(lr)) then
194 lr_wfns_are_associated = (allocated(lr%ddl_psi) .and. states_are_real(st)) .or. &
195 (allocated(lr%zdl_psi) .and. states_are_complex(st))
196 assert(lr_wfns_are_associated)
197 end if
198
200
201
202 iunit_states = restart_open(restart, 'states')
203 write(lines(1), '(a20,1i10)') 'nst= ', st%nst
204 write(lines(2), '(a20,1i10)') 'dim= ', st%d%dim
205 write(lines(3), '(a20,1i10)') 'nik= ', st%nik
206 call restart_write(restart, iunit_states, lines, 3, err)
207 if (err /= 0) ierr = ierr + 1
208 call restart_close(restart, iunit_states)
209
210
211 iunit_wfns = restart_open(restart, 'wfns')
212 lines(1) = '# #k-point #st #dim filename'
213 if (states_are_real(st)) then
214 lines(2) = '%Real_Wavefunctions'
215 else
216 lines(2) = '%Complex_Wavefunctions'
217 end if
218 call restart_write(restart, iunit_wfns, lines, 2, err)
219 if (err /= 0) ierr = ierr + 2
220
221
222 iunit_occs = restart_open(restart, 'occs')
223 lines(1) = '# occupations | eigenvalue[a.u.] | Im(eigenvalue) [a.u.] | k-points | k-weights | filename | ik | ist | idim'
224 lines(2) = '%Occupations_Eigenvalues_K-Points'
225 call restart_write(restart, iunit_occs, lines, 2, err)
226 if (err /= 0) ierr = ierr + 4
227
228
229 if (states_are_real(st)) then
230 safe_allocate(dpsi(1:mesh%np))
231 safe_allocate(rff_global(1:mesh%np_global))
232 else
233 safe_allocate(zpsi(1:mesh%np))
234 safe_allocate(zff_global(1:mesh%np_global))
235 end if
236
237 itot = 1
238 root = -1
239 err2 = 0
240 do ik = 1, st%nik
241 kpoint(1:space%dim) = &
242 kpoints%get_point(st%d%get_kpoint_index(ik), absolute_coordinates = .true.)
243
244 do ist = 1, st%nst
245 do idim = 1, st%d%dim
246 root(p_strategy_domains) = mod(itot - 1, mesh%mpi_grp%size)
247 write(filename,'(i10.10)') itot
248
249 write(lines(1), '(i8,a,i8,a,i8,3a)') ik, ' | ', ist, ' | ', idim, ' | "', trim(filename), '"'
250 call restart_write(restart, iunit_wfns, lines, 1, err)
251 if (err /= 0) err2(1) = err2(1) + 1
252
253 write(lines(1), '(e23.16,a,e23.16)') st%occ(ist,ik), ' | ', st%eigenval(ist, ik)
254 write(lines(1), '(a,a,e23.16)') trim(lines(1)), ' | ', m_zero
255 do idir = 1, space%dim
256 write(lines(1), '(a,a,e23.16)') trim(lines(1)), ' | ', kpoint(idir)
257 end do
258 write(lines(1), '(a,a,e23.16,a,i10.10,3(a,i8))') trim(lines(1)), &
259 ' | ', st%kweights(ik), ' | ', itot, ' | ', ik, ' | ', ist, ' | ', idim
260 call restart_write(restart, iunit_occs, lines, 1, err)
261 if (err /= 0) err2(1) = err2(1) + 1
262
263 should_write = st%st_start <= ist .and. ist <= st%st_end
264 if (should_write .and. present(st_start_writing)) then
265 if (ist < st_start_writing) should_write = .false.
266 end if
267
268 if (should_write) then
269 if (.not. present(lr)) then
270 if (st%d%kpt%start <= ik .and. ik <= st%d%kpt%end) then
271 if (states_are_real(st)) then
272 call states_elec_get_state(st, mesh, idim, ist, ik, dpsi)
273 call restart_write_mesh_function(restart, space, filename, mesh, dpsi, err, root = root)
274 else
275 call states_elec_get_state(st, mesh, idim, ist, ik, zpsi)
276 call restart_write_mesh_function(restart, space, filename, mesh, zpsi, err, root = root)
277 end if
278 else
279 err = 0
280 end if
281 else
282 if (st%d%kpt%start <= ik .and. ik <= st%d%kpt%end) then
283 if (states_are_real(st)) then
284 call restart_write_mesh_function(restart, space, filename, mesh, &
285 lr%ddl_psi(:, idim, ist, ik), err, root = root)
286 else
287 call restart_write_mesh_function(restart, space, filename, mesh, &
288 lr%zdl_psi(:, idim, ist, ik), err, root = root)
289 end if
290 else
291 err = 0
292 end if
293 end if
294 if (err /= 0) err2(2) = err2(2) + 1
295 end if
296
297 itot = itot + 1
298 end do ! st%d%dim
299 end do ! st%nst
300 end do ! st%nik
301 if (err2(1) /= 0) ierr = ierr + 8
302 if (err2(2) /= 0) ierr = ierr + 16
303
304 safe_deallocate_a(dpsi)
305 safe_deallocate_a(zpsi)
306 safe_deallocate_a(rff_global)
307 safe_deallocate_a(zff_global)
308
309 lines(1) = '%'
310 call restart_write(restart, iunit_occs, lines, 1, err)
311 if (err /= 0) ierr = ierr + 32
312 call restart_write(restart, iunit_wfns, lines, 1, err)
313 if (err /= 0) ierr = ierr + 64
314 if (present(iter)) then
315 write(lines(1),'(a,i7)') 'Iter = ', iter
316 call restart_write(restart, iunit_wfns, lines, 1, err)
317 if (err /= 0) ierr = ierr + 128
318 end if
319
320 call restart_close(restart, iunit_wfns)
321 call restart_close(restart, iunit_occs)
322
323 if (verbose_) then
324 message(1) = "Info: Finished writing states."
325 call print_date(trim(message(1))//' ')
326 end if
327
329
330 call profiling_out("RESTART_WRITE")
331 pop_sub(states_elec_dump)
332 return
333 end subroutine states_elec_dump
334
335
336 ! ---------------------------------------------------------
341 subroutine states_elec_load(restart, namespace, space, st, mesh, kpoints, ierr, iter, lr, lowest_missing, label, verbose, skip)
342 type(restart_t), intent(in) :: restart
343 type(namespace_t), intent(in) :: namespace
344 class(space_t), intent(in) :: space
345 type(states_elec_t), intent(inout) :: st
346 class(mesh_t), intent(in) :: mesh
347 type(kpoints_t), intent(in) :: kpoints
348 integer, intent(out) :: ierr
349 integer, optional, intent(out) :: iter
350 type(lr_t), optional, intent(inout) :: lr
351 integer, optional, intent(out) :: lowest_missing(:, :)
352 character(len=*), optional, intent(in) :: label
353 logical, optional, intent(in) :: verbose
354 logical, optional, intent(in) :: skip(:)
355
356 integer :: states_elec_file, wfns_file, occ_file, err, ik, ist, idir, idim
357 integer :: idone, iread, ntodo
358 character(len=12) :: filename
359 character(len=1) :: char
360 logical, allocatable :: filled(:, :, :)
361 character(len=256) :: lines(3), label_
362 character(len=50) :: str
363
364 real(real64) :: my_occ, imev, my_kweight
365 logical :: read_occ, lr_allocated, verbose_
366 logical :: integral_occs
367 real(real64), allocatable :: dpsi(:)
368 complex(real64), allocatable :: zpsi(:), zpsil(:)
369 character(len=256), allocatable :: restart_file(:, :, :)
370 logical, allocatable :: restart_file_present(:, :, :)
371 real(real64) :: kpoint(space%dim), read_kpoint(space%dim)
372
373 integer :: iread_tmp
374 integer, allocatable :: lowest_missing_tmp(:, :)
375
376 push_sub(states_elec_load)
377
378 ierr = 0
379
380 ! make sure these intent(out)`s are initialized no matter what
381 if (present(lowest_missing)) lowest_missing = 1
382 if (present(iter)) iter = 0
383
384 if (present(skip)) then
385 assert(ubound(skip, dim = 1) == st%nst)
386 end if
387
388 if (restart_skip(restart)) then
389 ierr = -1
390 pop_sub(states_elec_load)
391 return
392 end if
393
394 call profiling_in("RESTART_READ")
395
396 verbose_ = optional_default(verbose, .true.)
397
398 if (present(label)) then
399 label_ = trim(label)
400 else
401 if (present(lr)) then
402 label_ = " for linear response"
403 else
404 label_ = ""
405 end if
406 end if
407
408 message(1) = 'Info: Reading states'
409 if (len(trim(label_)) > 0) then
410 message(1) = trim(message(1)) // trim(label_)
411 end if
412 message(1) = trim(message(1)) // "."
413 if (verbose_) call print_date(trim(message(1))//' ')
414
415 if (.not. present(lr)) then
416 st%fromScratch = .false. ! obviously, we are using restart info
417 end if
418
419 ! If one restarts a GS calculation changing the %Occupations block, one
420 ! cannot read the occupations, otherwise these overwrite the ones from
421 ! the input file. restart_fixed_occ makes that we do use the ones in the file.
422 integral_occs = .true. ! only used if restart_fixed_occ
423 if (st%restart_fixed_occ) then
424 read_occ = .true.
425 st%fixed_occ = .true.
426 else
427 read_occ = .not. st%fixed_occ
428 end if
429
430 if (.not. present(lr)) then
431 st%eigenval(:, :) = m_zero
432 ! to be filled in from reading afterward
433 end if
435 if (.not. present(lr) .and. read_occ) then
436 st%occ(:, :) = m_zero
437 ! to be filled in from reading afterward
438 end if
439
440 ! sanity check
441 if (present(lr)) then
442 lr_allocated = (allocated(lr%ddl_psi) .and. states_are_real(st)) .or. &
443 (allocated(lr%zdl_psi) .and. states_are_complex(st))
444 assert(lr_allocated)
445 end if
446
447 states_elec_file = restart_open(restart, 'states')
448 ! sanity check on spin/k-points. Example file 'states':
449 ! nst= 2
450 ! dim= 1
451 ! nik= 2
452 call restart_read(restart, states_elec_file, lines, 3, err)
453 if (err /= 0) then
454 ierr = ierr - 2
455 else
456 read(lines(2), *) str, idim
457 read(lines(3), *) str, ik
458 if (idim == 2 .and. st%d%dim == 1) then
459 write(message(1),'(a)') 'Incompatible restart information: saved calculation is spinors, this one is not.'
460 call messages_warning(1, namespace=namespace)
461 ierr = ierr - 2**2
462 end if
463 if (idim == 1 .and. st%d%dim == 2) then
464 write(message(1),'(a)') 'Incompatible restart information: this calculation is spinors, saved one is not.'
465 call messages_warning(1, namespace=namespace)
466 ierr = ierr - 2**3
467 end if
468 if (ik < st%nik) then
469 write(message(1),'(a)') 'Incompatible restart information: not enough k-points.'
470 write(message(2),'(2(a,i6))') 'Expected ', st%nik, ' > Read ', ik
471 call messages_warning(2, namespace=namespace)
472 end if
473 ! We will check that they are the right k-points later, so we do not need to put a specific error here.
474 end if
475 call restart_close(restart, states_elec_file)
476
477
478 ! open files to read
479 wfns_file = restart_open(restart, 'wfns')
480 occ_file = restart_open(restart, 'occs')
481 call restart_read(restart, wfns_file, lines, 2, err)
482 if (err /= 0) then
483 ierr = ierr - 2**5
484 else if (states_are_real(st)) then
485 read(lines(2), '(a)') str
486 if (str(2:8) == 'Complex') then
487 message(1) = "Cannot read real states from complex wavefunctions."
488 call messages_warning(1, namespace=namespace)
489 ierr = ierr - 2**6
490 else if (str(2:5) /= 'Real') then
491 message(1) = "Restart file 'wfns' does not specify real/complex; cannot check compatibility."
492 call messages_warning(1, namespace=namespace)
493 end if
494 end if
495 ! complex can be restarted from real, so there is no problem.
496
497 ! Skip two lines.
498 call restart_read(restart, occ_file, lines, 2, err)
499 if (err /= 0) ierr = ierr - 2**7
500
501 ! If any error occured up to this point then it is not worth continuing,
502 ! as there something fundamentally wrong with the restart files
503 if (ierr /= 0) then
504 call restart_close(restart, wfns_file)
505 call restart_close(restart, occ_file)
506 call profiling_out("RESTART_READ")
507 pop_sub(states_elec_load)
508 return
509 end if
510
511 if (states_are_real(st)) then
512 safe_allocate(dpsi(1:mesh%np))
513 else
514 safe_allocate(zpsi(1:mesh%np))
515 end if
516
517 safe_allocate(restart_file(1:st%d%dim, st%st_start:st%st_end, 1:st%nik))
518 safe_allocate(restart_file_present(1:st%d%dim, st%st_start:st%st_end, 1:st%nik))
519 restart_file_present = .false.
520
521 ! Next we read the list of states from the files.
522 ! Errors in reading the information of a specific state from the files are ignored
523 ! at this point, because later we will skip reading the wavefunction of that state.
524 do
525 call restart_read(restart, wfns_file, lines, 1, err)
526 if (err == 0) then
527 read(lines(1), '(a)') char
528 if (char == '%') then
529 !We reached the end of the file
530 exit
531 else
532 read(lines(1), *) ik, char, ist, char, idim, char, filename
533 end if
534 end if
535
536 if (err /= 0 .or. index_is_wrong()) then
537 call restart_read(restart, occ_file, lines, 1, err)
538 cycle
539 end if
540
541 if (ist >= st%st_start .and. ist <= st%st_end .and. &
542 ik >= st%d%kpt%start .and. ik <= st%d%kpt%end) then
543
544 restart_file(idim, ist, ik) = trim(filename)
545 restart_file_present(idim, ist, ik) = .true.
546 end if
547
548 call restart_read(restart, occ_file, lines, 1, err)
549 if (.not. present(lr)) then ! do not read eigenvalues or occupations when reading linear response
550 ! # occupations | eigenvalue[a.u.] | Im(eigenvalue) [a.u.] | k-points | k-weights | filename | ik | ist | idim
551
552 if (err == 0) then
553 read(lines(1), *) my_occ, char, st%eigenval(ist, ik), char, imev, char, &
554 (read_kpoint(idir), char, idir = 1, space%dim), my_kweight
555 ! we do not want to read the k-weights, we have already set them appropriately
556 else
557 ! There is a problem with this states information, so we skip it.
558 if (ist >= st%st_start .and. ist <= st%st_end .and. &
559 ik >= st%d%kpt%start .and. ik <= st%d%kpt%end) then
560 restart_file_present(idim, ist, ik) = .false.
561 end if
562 cycle
563 end if
564
565 kpoint(1:space%dim) = &
566 kpoints%get_point(st%d%get_kpoint_index(ik), absolute_coordinates = .true.)
567 ! FIXME: maybe should ignore ik and just try to match actual vector k-points?
568 if (any(abs(kpoint(1:space%dim) - read_kpoint(1:space%dim)) > 1e-12_real64)) then
569 ! write only once for each k-point so as not to be too verbose
570 if (ist == 1) then
571 write(message(1),'(a,i6)') 'Incompatible restart information: k-point mismatch for ik ', ik
572 write(message(2),'(a,99f18.12)') ' Expected : ', kpoint(1:space%dim)
573 write(message(3),'(a,99f18.12)') ' Read : ', read_kpoint(1:space%dim)
574 call messages_warning(3, namespace=namespace)
575 end if
576 if (ist >= st%st_start .and. ist <= st%st_end .and. &
577 ik >= st%d%kpt%start .and. ik <= st%d%kpt%end) then
578 restart_file_present(idim, ist, ik) = .false.
579 end if
580 end if
581
582 if (read_occ) then
583 st%occ(ist, ik) = my_occ
584 integral_occs = integral_occs .and. &
585 abs((st%occ(ist, ik) - st%smear%el_per_state) * st%occ(ist, ik)) <= m_epsilon
586 end if
587 end if
588 end do
589
590 if (present(iter)) then
591 call restart_read(restart, wfns_file, lines, 1, err)
592 if (err /= 0) then
593 ierr = ierr - 2**8
594 else
595 read(lines(1), *) filename, filename, iter
596 end if
597 end if
598
599 call restart_close(restart, wfns_file)
600 call restart_close(restart, occ_file)
601
602 ! Now we read the wavefunctions. At this point we need to have all the information from the
603 ! states, occs, and wfns files in order to avoid serialisation of reads, as restart_read
604 ! works as a barrier.
605
606 safe_allocate(filled(1:st%d%dim, st%st_start:st%st_end, st%d%kpt%start:st%d%kpt%end))
607 filled = .false.
608
609 if (present(lowest_missing)) lowest_missing = st%nst + 1
610
611 iread = 0
612 if (mpi_grp_is_root(mpi_world) .and. verbose_) then
613 idone = 0
614 ntodo = st%lnst*st%d%kpt%nlocal*st%d%dim
615 call loct_progress_bar(-1, ntodo)
616 end if
617
618 do ik = st%d%kpt%start, st%d%kpt%end
619 do ist = st%st_start, st%st_end
620 if (present(skip)) then
621 if (skip(ist)) cycle
622 end if
623
624 do idim = 1, st%d%dim
625
626 if (.not. restart_file_present(idim, ist, ik)) then
627 if (present(lowest_missing)) then
628 lowest_missing(idim, ik) = min(lowest_missing(idim, ik), ist)
629 end if
630 cycle
631 end if
632
633 if (states_are_real(st)) then
634 call restart_read_mesh_function(restart, space, restart_file(idim, ist, ik), mesh, dpsi, err)
635 else
636 call restart_read_mesh_function(restart, space, restart_file(idim, ist, ik), mesh, zpsi, err)
637 end if
638
639 if (states_are_real(st)) then
640 if (.not. present(lr)) then
641 call states_elec_set_state(st, mesh, idim, ist, ik, dpsi)
642 else
643 call lalg_copy(mesh%np, dpsi, lr%ddl_psi(:, idim, ist, ik))
644 end if
645 else
646 if (.not. present(lr)) then
647 call states_elec_set_state(st, mesh, idim, ist, ik, zpsi)
648 else
649 call lalg_copy(mesh%np, zpsi, lr%zdl_psi(:, idim, ist, ik))
650 end if
651 end if
652
653
654 if (err == 0) then
655 filled(idim, ist, ik) = .true.
656 iread = iread + 1
657 else if (present(lowest_missing)) then
658 lowest_missing(idim, ik) = min(lowest_missing(idim, ik), ist)
659 end if
660
661 if (mpi_grp_is_root(mpi_world) .and. verbose_) then
662 idone = idone + 1
663 call loct_progress_bar(idone, ntodo)
664 end if
665
666 end do
667 end do
668 end do
669
670 safe_deallocate_a(dpsi)
671 safe_deallocate_a(zpsi)
672 safe_deallocate_a(zpsil)
673 safe_deallocate_a(restart_file)
674 safe_deallocate_a(restart_file_present)
675
676 if (mpi_grp_is_root(mpi_world) .and. verbose_) then
677 call messages_new_line()
678 end if
679
680 if (st%parallel_in_states .or. st%d%kpt%parallel) then
681 iread_tmp = iread
682 call st%st_kpt_mpi_grp%allreduce(iread_tmp, iread, 1, mpi_integer, mpi_sum)
683 end if
684
685 if (st%d%kpt%parallel) then
686 ! make sure all tasks know lowest_missing over all k-points
687 if (present(lowest_missing)) then
688 safe_allocate(lowest_missing_tmp(1:st%d%dim, 1:st%nik))
689 lowest_missing_tmp = lowest_missing
690 call st%d%kpt%mpi_grp%allreduce(lowest_missing_tmp(1,1), lowest_missing(1,1), st%d%dim*st%nik, &
691 mpi_integer, mpi_min)
692 safe_deallocate_a(lowest_missing_tmp)
693 end if
694 end if
695
696 if (st%restart_fixed_occ .and. iread == st%nst * st%nik * st%d%dim) then
697 ! reset to overwrite whatever smearing may have been set earlier
698 ! only do this if all states could be loaded from restart files
699 call smear_init(st%smear, namespace, st%d%ispin, fixed_occ = .true., integral_occs = integral_occs, kpoints = kpoints)
700 end if
701
702 if (.not. present(lr) .and. .not. present(skip)) call fill_random()
703 ! it is better to initialize lr wfns to zero
704
705 safe_deallocate_a(filled)
706
707 if (ierr == 0 .and. iread /= st%nst * st%nik * st%d%dim) then
708 if (iread > 0) then
709 ierr = iread
710 else
711 ierr = -1
712 end if
713 ! otherwise ierr = 0 would mean either all was read correctly, or nothing at all was read!
714
715 if (.not. present(lr)) then
716 write(str, '(a,i5)') 'Reading states.'
717 else
718 write(str, '(a,i5)') 'Reading states information for linear response.'
719 end if
720 call messages_print_with_emphasis(msg=trim(str), namespace=namespace)
721 if (.not. present(skip)) then
722 write(message(1),'(a,i6,a,i6,a)') 'Only ', iread,' files out of ', &
723 st%nst * st%nik * st%d%dim, ' could be read.'
724 else
725 write(message(1),'(a,i6,a,i6,a)') 'Only ', iread,' files out of ', &
726 st%nst * st%nik * st%d%dim, ' were loaded.'
727 ierr = 0
728 end if
729 call messages_info(1, namespace=namespace)
730 call messages_print_with_emphasis(namespace=namespace)
731 end if
732
733 message(1) = 'Info: States reading done.'
734 if (verbose_) call print_date(trim(message(1))//' ')
735
736 call profiling_out("RESTART_READ")
737 pop_sub(states_elec_load)
738
739 contains
740
741 ! ---------------------------------------------------------
742 subroutine fill_random() !< put random function in orbitals that could not be read.
744
745 do ik = st%d%kpt%start, st%d%kpt%end
746
747 do ist = st%st_start, st%st_end
748 do idim = 1, st%d%dim
749 if (filled(idim, ist, ik)) cycle
750
751 call states_elec_generate_random(st, mesh, kpoints, ist, ist, ik, ik)
752 end do
753 end do
754 end do
755
757 end subroutine fill_random
758
759 ! ---------------------------------------------------------
760
761 logical function index_is_wrong() !< .true. if the index (idim, ist, ik) is not present in st structure...
763
764 if (idim > st%d%dim .or. idim < 1 .or. &
765 ist > st%nst .or. ist < 1 .or. &
766 ik > st%nik .or. ik < 1) then
768 else
769 index_is_wrong = .false.
770 end if
771
773 end function index_is_wrong
774
775 end subroutine states_elec_load
776
777
778 subroutine states_elec_dump_rho(restart, space, st, mesh, ierr, iter)
779 type(restart_t), intent(in) :: restart
780 class(space_t), intent(in) :: space
781 type(states_elec_t), intent(in) :: st
782 class(mesh_t), intent(in) :: mesh
783 integer, intent(out) :: ierr
784 integer, optional, intent(in) :: iter
785
786 integer :: iunit, isp, err, err2(2)
787 character(len=MAX_PATH_LEN) :: filename
788 character(len=300) :: lines(2)
789
790 push_sub(states_elec_dump_rho)
791
792 ierr = 0
793
794 if (restart_skip(restart)) then
795 pop_sub(states_elec_dump_rho)
796 return
797 end if
798
799 message(1) = "Debug: Writing density restart."
800 call messages_info(1, debug_only=.true.)
801
803
804 !write the densities
805 iunit = restart_open(restart, 'density')
806 lines(1) = '# #spin #nspin filename'
807 lines(2) = '%densities'
808 call restart_write(restart, iunit, lines, 2, err)
809 if (err /= 0) ierr = ierr + 1
810
811 err2 = 0
812 do isp = 1, st%d%nspin
813 filename = get_filename_with_spin('density', st%d%nspin, isp)
814 write(lines(1), '(i8,a,i8,a)') isp, ' | ', st%d%nspin, ' | "'//trim(adjustl(filename))//'"'
815 call restart_write(restart, iunit, lines, 1, err)
816 if (err /= 0) err2(1) = err2(1) + 1
817
818 call restart_write_mesh_function(restart, space, filename, mesh, st%rho(:,isp), err)
819 if (err /= 0) err2(2) = err2(2) + 1
820
821 end do
822 if (err2(1) /= 0) ierr = ierr + 2
823 if (err2(2) /= 0) ierr = ierr + 4
824
825 lines(1) = '%'
826 call restart_write(restart, iunit, lines, 1, err)
827 if (err /= 0) ierr = ierr + 8
828 if (present(iter)) then
829 write(lines(1),'(a,i7)') 'Iter = ', iter
830 call restart_write(restart, iunit, lines, 1, err)
831 if (err /= 0) ierr = ierr + 16
832 end if
833 call restart_close(restart, iunit)
834
836
837 message(1) = "Debug: Writing density restart done."
838 call messages_info(1, debug_only=.true.)
839
840 pop_sub(states_elec_dump_rho)
841 end subroutine states_elec_dump_rho
842
843
844 ! ---------------------------------------------------------
845 subroutine states_elec_load_rho(restart, space, st, mesh, ierr)
846 type(restart_t), intent(in) :: restart
847 class(space_t), intent(in) :: space
848 type(states_elec_t), intent(inout) :: st
849 class(mesh_t), intent(in) :: mesh
850 integer, intent(out) :: ierr
851
852 integer :: err, err2, isp
853 character(len=MAX_PATH_LEN) :: filename
855 push_sub(states_elec_load_rho)
856
857 ierr = 0
858
859 if (restart_skip(restart)) then
860 ierr = -1
861 pop_sub(states_elec_load_rho)
862 return
863 end if
864
865 message(1) = "Debug: Reading density restart."
866 call messages_info(1, debug_only=.true.)
867
868 ! skip for now, since we know what the files are going to be called
869 !read the densities
870! iunit_rho = io_open(trim(dir)//'/density', restart%namespace, action='write')
871! call iopar_read(st%dom_st_kpt_mpi_grp, iunit_rho, line, err)
872! call iopar_read(st%dom_st_kpt_mpi_grp, iunit_rho, line, err)
873! we could read the iteration 'iter' too, not sure if that is useful.
874
875 err2 = 0
876 do isp = 1, st%d%nspin
877 filename = get_filename_with_spin('density', st%d%nspin, isp)
878! if (mpi_grp_is_root(st%dom_st_kpt_mpi_grp)) then
879! read(iunit_rho, '(i8,a,i8,a)') isp, ' | ', st%d%nspin, ' | "'//trim(adjustl(filename))//'"'
880! end if
881 call restart_read_mesh_function(restart, space, filename, mesh, st%rho(:,isp), err)
882 if (err /= 0) err2 = err2 + 1
883
884 end do
885 if (err2 /= 0) ierr = ierr + 1
886
887 message(1) = "Debug: Reading density restart done."
888 call messages_info(1, debug_only=.true.)
889
890 pop_sub(states_elec_load_rho)
891 end subroutine states_elec_load_rho
892
893 subroutine states_elec_dump_frozen(restart, space, st, mesh, ierr)
894 type(restart_t), intent(in) :: restart
895 class(space_t), intent(in) :: space
896 type(states_elec_t), intent(in) :: st
897 class(mesh_t), intent(in) :: mesh
898 integer, intent(out) :: ierr
899
900 integer :: isp, err, err2(2), idir
901 character(len=MAX_PATH_LEN) :: filename
902
904
905 ierr = 0
906
907 assert(allocated(st%frozen_rho))
908
909 if (restart_skip(restart)) then
911 return
912 end if
913
914 message(1) = "Debug: Writing frozen densities restart."
915 call messages_info(1, debug_only=.true.)
916
918
919 err2 = 0
920 do isp = 1, st%d%nspin
921 filename = get_filename_with_spin('frozen_rho', st%d%nspin, isp)
922
923 call restart_write_mesh_function(restart, space, filename, mesh, st%frozen_rho(:,isp), err)
924 if (err /= 0) err2(2) = err2(2) + 1
925
926 if (allocated(st%frozen_tau)) then
927 filename = get_filename_with_spin('frozen_tau', st%d%nspin, isp)
928 call restart_write_mesh_function(restart, space, filename, mesh, st%frozen_tau(:,isp), err)
929 if (err /= 0) err2 = err2 + 1
930 end if
931
932 if (allocated(st%frozen_gdens)) then
933 do idir = 1, space%dim
934 if (st%d%nspin == 1) then
935 write(filename, fmt='(a,i1)') 'frozen_gdens-dir', idir
936 else
937 write(filename, fmt='(a,i1,a,i1)') 'frozen_tau-dir', idir, '-', isp
938 end if
939 call restart_write_mesh_function(restart, space, filename, mesh, st%frozen_gdens(:,idir,isp), err)
940 if (err /= 0) err2 = err2 + 1
941 end do
942 end if
943
944 if (allocated(st%frozen_ldens)) then
945 filename = get_filename_with_spin('frozen_ldens', st%d%nspin, isp)
946 call restart_write_mesh_function(restart, space, filename, mesh, st%frozen_ldens(:,isp), err)
947 if (err /= 0) err2 = err2 + 1
948 end if
949
950 end do
951 if (err2(1) /= 0) ierr = ierr + 2
952 if (err2(2) /= 0) ierr = ierr + 4
953
955
956 message(1) = "Debug: Writing frozen densities restart done."
957 call messages_info(1, debug_only=.true.)
958
960 end subroutine states_elec_dump_frozen
961
962
963 ! ---------------------------------------------------------
964 subroutine states_elec_load_frozen(restart, space, st, mesh, ierr)
965 type(restart_t), intent(in) :: restart
966 class(space_t), intent(in) :: space
967 type(states_elec_t), intent(inout) :: st
968 class(mesh_t), intent(in) :: mesh
969 integer, intent(out) :: ierr
970
971 integer :: err, err2, isp, idir
972 character(len=MAX_PATH_LEN) :: filename
973
975
976 assert(allocated(st%frozen_rho))
977
978 ierr = 0
979
980 if (restart_skip(restart)) then
981 ierr = -1
983 return
984 end if
985
986 message(1) = "Debug: Reading densities restart."
987 call messages_info(1, debug_only=.true.)
988
989 err2 = 0
990 do isp = 1, st%d%nspin
991 filename = get_filename_with_spin('frozen_rho', st%d%nspin, isp)
992 call restart_read_mesh_function(restart, space, filename, mesh, st%frozen_rho(:,isp), err)
993 if (err /= 0) err2 = err2 + 1
994
995 if (allocated(st%frozen_tau)) then
996 filename = get_filename_with_spin('frozen_tau', st%d%nspin, isp)
997 call restart_read_mesh_function(restart, space, filename, mesh, st%frozen_tau(:,isp), err)
998 if (err /= 0) err2 = err2 + 1
999 end if
1000
1001 if (allocated(st%frozen_gdens)) then
1002 do idir = 1, space%dim
1003 if (st%d%nspin == 1) then
1004 write(filename, fmt='(a,i1)') 'frozen_gdens-dir', idir
1005 else
1006 write(filename, fmt='(a,i1,a,i1)') 'frozen_tau-dir', idir, '-', isp
1007 end if
1008 call restart_read_mesh_function(restart, space, filename, mesh, st%frozen_gdens(:,idir,isp), err)
1009 if (err /= 0) err2 = err2 + 1
1010 end do
1011 end if
1012
1013 if (allocated(st%frozen_ldens)) then
1014 filename = get_filename_with_spin('frozen_ldens', st%d%nspin, isp)
1015 call restart_read_mesh_function(restart, space, filename, mesh, st%frozen_ldens(:,isp), err)
1016 if (err /= 0) err2 = err2 + 1
1017 end if
1018
1019 end do
1020 if (err2 /= 0) ierr = ierr + 1
1021
1022 message(1) = "Debug: Reading frozen densities restart done."
1023 call messages_info(1, debug_only=.true.)
1024
1026 end subroutine states_elec_load_frozen
1027
1028
1029 ! ---------------------------------------------------------
1032 subroutine states_elec_read_user_def_orbitals(mesh, namespace, space, st)
1033 class(mesh_t), intent(in) :: mesh
1034 type(namespace_t), intent(in) :: namespace
1035 class(space_t), intent(in) :: space
1036 type(states_elec_t), intent(inout) :: st
1037
1038 type(block_t) :: blk
1039 integer :: ip, id, is, ik, nstates, state_from, ierr, ncols
1040 integer :: ib, idim, inst, inik, normalize
1041 real(real64) :: xx(space%dim), rr, psi_re, psi_im
1042 character(len=150) :: filename
1043 complex(real64), allocatable :: zpsi(:, :)
1044
1045 integer, parameter :: &
1046 state_from_formula = 1, &
1047 state_from_file = -10010, &
1048 normalize_yes = 1, &
1049 normalize_no = 0
1050
1052
1053 !%Variable UserDefinedStates
1054 !%Type block
1055 !%Section States
1056 !%Description
1057 !% Instead of using the ground state as initial state for
1058 !% time-propagations it might be interesting in some cases
1059 !% to specify alternate states. Like with user-defined
1060 !% potentials, this block allows you to specify formulas for
1061 !% the orbitals at <i>t</i>=0.
1062 !%
1063 !% Example:
1064 !%
1065 !% <tt>%UserDefinedStates
1066 !% <br>&nbsp;&nbsp; 1 | 1 | 1 | formula | "exp(-r^2)*exp(-i*0.2*x)" | normalize_yes
1067 !% <br>%</tt>
1068 !%
1069 !% The first column specifies the component of the spinor,
1070 !% the second column the number of the state and the third
1071 !% contains <i>k</i>-point and spin quantum numbers. Column four
1072 !% indicates that column five should be interpreted as a formula
1073 !% for the corresponding orbital.
1074 !%
1075 !% Alternatively, if column four states <tt>file</tt> the state will
1076 !% be read from the file given in column five.
1077 !%
1078 !% <tt>%UserDefinedStates
1079 !% <br>&nbsp;&nbsp; 1 | 1 | 1 | file | "/path/to/file" | normalize_no
1080 !% <br>%</tt>
1081 !%
1082 !% Octopus reads first the ground-state orbitals from
1083 !% the <tt>restart/gs</tt> directory. Only the states that are
1084 !% specified in the above block will be overwritten with
1085 !% the given analytic expression for the orbital.
1086 !%
1087 !% The sixth (optional) column indicates whether <tt>Octopus</tt> should renormalize
1088 !% the orbital. The default (no sixth column given) is to renormalize.
1089 !%
1090 !%Option file -10010
1091 !% Read initial orbital from file.
1092 !% Accepted file formats, detected by extension: obf, ncdf and csv (real only).
1093 !% For csv files, the following formatting rules apply:
1094 !%
1095 !% The functions in this file read an array from an ascii matrix (csv) file.
1096 !% Format with values "valueXYZ" as follows
1097 !%
1098 !% File values.csv:
1099 !% --------
1100 !% value111 value211 value311 value411
1101 !% value121 value221 value321 value421
1102 !% value131 value231 value331 value431
1103 !%
1104 !% value112 value212 value312 value412
1105 !% value122 value222 value322 value422
1106 !% value132 value232 value332 value432
1107 !%
1108 !% value113 value213 value313 value413
1109 !% value123 value223 value323 value423
1110 !% value133 value233 value333 value433
1111 !% --------
1112 !%
1113 !% That is, every line scans the x-coordinate, every XY-plane as a table of values
1114 !% and all XY-planes separated by an empty row.
1115 !%
1116 !% The given matrix is interpolated/stretched to fit the calculation
1117 !% box defined in input file.
1118 !%
1119 !% Note that there must not be any empty line at the end of the file.
1120 !%
1121 !% Calculation box shape must be "parallelepiped".
1122 !%
1123 !% The delimiter can be a tab, a comma or a space.
1124 !%Option formula 1
1125 !% Calculate initial orbital by given analytic expression.
1126 !%Option normalize_yes 1
1127 !% Normalize orbitals (default).
1128 !%Option normalize_no 0
1129 !% Do not normalize orbitals.
1130 !%End
1131 if (parse_block(namespace, 'UserDefinedStates', blk) == 0) then
1132
1133 call messages_print_with_emphasis(msg=trim('Substitution of orbitals'), namespace=namespace)
1134
1135 ! find out how many lines (i.e. states) the block has
1136 nstates = parse_block_n(blk)
1137
1138 safe_allocate(zpsi(1:mesh%np, 1:st%d%dim))
1139
1140 ! read all lines
1141 do ib = 1, nstates
1142 ! Check that number of columns is five or six.
1143 ncols = parse_block_cols(blk, ib - 1)
1144 if (ncols < 5 .or. ncols > 6) then
1145 message(1) = 'Each line in the UserDefinedStates block must have'
1146 message(2) = 'five or six columns.'
1147 call messages_fatal(2, namespace=namespace)
1148 end if
1149
1150 call parse_block_integer(blk, ib - 1, 0, idim)
1151 call parse_block_integer(blk, ib - 1, 1, inst)
1152 call parse_block_integer(blk, ib - 1, 2, inik)
1153
1154 ! Calculate from expression or read from file?
1155 call parse_block_integer(blk, ib - 1, 3, state_from)
1156
1157 ! loop over all states
1158 do id = 1, st%d%dim
1159 do is = 1, st%nst
1160 do ik = 1, st%nik
1161
1162 if (.not.(id == idim .and. is == inst .and. ik == inik )) cycle
1163
1164 ! We first parse the value, such that the parsing and stdout looks correct
1165 ! However, if rank 0 is not responsible for the parsing, the variables in the formula
1166 ! will still appear as not parsed (pure k-point/state parallelization case).
1167 ! This could be solved by a dummy call to parse_expression
1168 select case (state_from)
1169 case (state_from_formula)
1170 ! parse formula string
1171 call parse_block_string( &
1172 blk, ib - 1, 4, st%user_def_states(id, is, ik))
1173
1174 write(message(1), '(a,3i5)') 'Substituting state of orbital with k, ist, dim = ', ik, is, id
1175 write(message(2), '(2a)') ' with the expression:'
1176 write(message(3), '(2a)') ' ',trim(st%user_def_states(id, is, ik))
1177 call messages_info(3, namespace=namespace)
1178
1179 ! convert to C string
1180 call conv_to_c_string(st%user_def_states(id, is, ik))
1181
1182 case (state_from_file)
1183 ! The input format can be coded in column four now. As it is
1184 ! not used now, we just say "file".
1185 ! Read the filename.
1186 call parse_block_string(blk, ib - 1, 4, filename)
1187
1188 write(message(1), '(a,3i5)') 'Substituting state of orbital with k, ist, dim = ', ik, is, id
1189 write(message(2), '(2a)') ' with data from file:'
1190 write(message(3), '(2a)') ' ',trim(filename)
1191 call messages_info(3, namespace=namespace)
1192 end select
1193
1194
1195 ! does the block entry match and is this node responsible?
1196 if (.not.(st%st_start <= is .and. st%st_end >= is &
1197 .and. st%d%kpt%start <= ik .and. st%d%kpt%end >= ik)) cycle
1198
1199 select case (state_from)
1200
1201 case (state_from_formula)
1202 ! fill states with user-defined formulas
1203 do ip = 1, mesh%np
1204 xx = mesh%x(ip, :)
1205 rr = norm2(xx)
1206
1207 ! parse user-defined expressions
1208 call parse_expression(psi_re, psi_im, space%dim, xx, rr, m_zero, st%user_def_states(id, is, ik))
1209 ! fill state
1210 zpsi(ip, 1) = cmplx(psi_re, psi_im, real64)
1211 end do
1212
1213 case (state_from_file)
1214 ! finally read the state
1215 call zio_function_input(filename, namespace, space, mesh, zpsi(:, 1), ierr)
1216 if (ierr > 0) then
1217 message(1) = 'Could not read the file!'
1218 write(message(2),'(a,i1)') 'Error code: ', ierr
1219 call messages_fatal(2, namespace=namespace)
1220 end if
1221
1222 case default
1223 message(1) = 'Wrong entry in UserDefinedStates, column 4.'
1224 message(2) = 'You may state "formula" or "file" here.'
1225 call messages_fatal(2, namespace=namespace)
1226 end select
1227
1228 ! normalize orbital
1229 if (parse_block_cols(blk, ib - 1) == 6) then
1230 call parse_block_integer(blk, ib - 1, 5, normalize)
1231 else
1232 normalize = 1
1233 end if
1234 select case (normalize)
1235 case (normalize_no)
1236 call states_elec_set_state(st, mesh, id, is, ik, zpsi(:, 1))
1237 case (normalize_yes)
1238 assert(st%d%dim == 1)
1239 call zmf_normalize(mesh, st%d%dim, zpsi)
1240 call states_elec_set_state(st, mesh, is, ik, zpsi)
1241 case default
1242 message(1) = 'The sixth column in UserDefinedStates may either be'
1243 message(2) = '"normalize_yes" or "normalize_no"'
1244 call messages_fatal(2, namespace=namespace)
1245 end select
1246
1247 end do
1248 end do
1249 end do
1250
1251 end do
1252
1253 safe_deallocate_a(zpsi)
1254
1255 call parse_block_end(blk)
1256 call messages_print_with_emphasis(namespace=namespace)
1257
1258 else
1259 call messages_variable_is_block(namespace, 'UserDefinedStates')
1260 end if
1261
1264
1265 ! ---------------------------------------------------------
1266 ! This is needed as for the generalized Bloch theorem we need to label
1267 ! the states from the expectation value of Sz computed from the GS.
1268 subroutine states_elec_dump_spin(restart, st, ierr)
1269 type(restart_t), intent(in) :: restart
1270 type(states_elec_t), intent(in) :: st
1271 integer, intent(out) :: ierr
1272
1273 integer :: iunit_spin
1274 integer :: err, err2(2), ik, ist
1275 character(len=300) :: lines(3)
1276
1277 push_sub(states_elec_dump_spin)
1278
1279 ierr = 0
1280
1281 if (restart_skip(restart)) then
1282 pop_sub(states_elec_dump_spin)
1283 return
1284 end if
1285
1286 call profiling_in("RESTART_WRITE_SPIN")
1287
1289
1290 iunit_spin = restart_open(restart, 'spin')
1291 lines(1) = '# #k-point #st #spin(x) spin(y) spin(z)'
1292 call restart_write(restart, iunit_spin, lines, 1, err)
1293 if (err /= 0) ierr = ierr + 1
1294
1295 err2 = 0
1296 do ik = 1, st%nik
1297 do ist = 1, st%nst
1298 write(lines(1), '(i8,a,i8,3(a,f18.12))') ik, ' | ', ist, ' | ', &
1299 st%spin(1,ist,ik), ' | ', st%spin(2,ist,ik),' | ', st%spin(3,ist,ik)
1300 call restart_write(restart, iunit_spin, lines, 1, err)
1301 if (err /= 0) err2(1) = err2(1) + 1
1302 end do ! st%nst
1303 end do ! st%nik
1304 lines(1) = '%'
1305 call restart_write(restart, iunit_spin, lines, 1, err)
1306 if (err2(1) /= 0) ierr = ierr + 8
1307 if (err2(2) /= 0) ierr = ierr + 16
1308
1309 call restart_close(restart, iunit_spin)
1310
1312
1313 call profiling_out("RESTART_WRITE_SPIN")
1314 pop_sub(states_elec_dump_spin)
1315 end subroutine states_elec_dump_spin
1316
1317
1318
1319
1320 ! ---------------------------------------------------------
1325 subroutine states_elec_load_spin(restart, st, ierr)
1326 type(restart_t), intent(in) :: restart
1327 type(states_elec_t), intent(inout) :: st
1328 integer, intent(out) :: ierr
1329
1330 integer :: spin_file, err, ik, ist
1331 character(len=256) :: lines(3)
1332 real(real64) :: spin(3)
1333 character(len=1) :: char
1334
1335
1336 push_sub(states_elec_load_spin)
1337
1338 ierr = 0
1339
1340 if (restart_skip(restart)) then
1341 ierr = -1
1342 pop_sub(states_elec_load_spin)
1343 return
1344 end if
1345
1346 call profiling_in("RESTART_READ_SPIN")
1347
1348 ! open files to read
1349 spin_file = restart_open(restart, 'spin')
1350 ! Skip two lines.
1351 call restart_read(restart, spin_file, lines, 1, err)
1352 if (err /= 0) ierr = ierr - 2**7
1353
1354 ! If any error occured up to this point then it is not worth continuing,
1355 ! as there something fundamentally wrong with the restart files
1356 if (ierr /= 0) then
1357 call restart_close(restart, spin_file)
1358 call profiling_out("RESTART_READ_SPIN")
1359 pop_sub(states_elec_load_spin)
1360 return
1361 end if
1362
1363 ! Next we read the list of states from the files.
1364 ! Errors in reading the information of a specific state from the files are ignored
1365 ! at this point, because later we will skip reading the wavefunction of that state.
1366 do
1367 call restart_read(restart, spin_file, lines, 1, err)
1368 read(lines(1), '(a)') char
1369 if (char == '%') then
1370 !We reached the end of the file
1371 exit
1372 else
1373 read(lines(1), *) ik, char, ist, char, spin(1), char, spin(2), char, spin(3)
1374 end if
1375
1376 if (err /= 0) cycle
1377
1378 st%spin(1:3, ist, ik) = spin(1:3)
1379 end do
1380
1381 call restart_close(restart, spin_file)
1382
1383 call profiling_out("RESTART_READ_SPIN")
1384 pop_sub(states_elec_load_spin)
1385 end subroutine states_elec_load_spin
1386
1387 ! ---------------------------------------------------------
1388 subroutine states_elec_transform(st, namespace, space, restart, mesh, kpoints, prefix)
1389 type(states_elec_t), intent(inout) :: st
1390 type(namespace_t), intent(in) :: namespace
1391 class(space_t), intent(in) :: space
1392 type(restart_t), intent(inout) :: restart
1393 class(mesh_t), intent(in) :: mesh
1394 type(kpoints_t), intent(in) :: kpoints
1395 character(len=*), optional, intent(in) :: prefix
1396
1397 type(states_elec_t) :: stin
1398 type(block_t) :: blk
1399 complex(real64), allocatable :: rotation_matrix(:,:), psi(:, :)
1400 integer :: ist, jst, ncols, iqn
1401 character(len=256) :: block_name
1402
1403 push_sub(states_elec_transform)
1404
1405 block_name = trim(optional_default(prefix, "")) // "TransformStates"
1406
1407 !%Variable TransformStates
1408 !%Type block
1409 !%Default no
1410 !%Section States
1411 !%Description
1412 !% Before starting the <tt>td</tt> calculation, the initial states (that are
1413 !% read from the <tt>restart/gs</tt> directory, which should have been
1414 !% generated in a previous ground-state calculation) can be "transformed"
1415 !% among themselves. The block <tt>TransformStates</tt> gives the transformation matrix
1416 !% to be used. The number of rows and columns of the matrix should equal the number
1417 !% of the states present in the time-dependent calculation (the independent
1418 !% spin and <i>k</i>-point subspaces are all transformed equally); the number of
1419 !% columns should be equal to the number of states present in the
1420 !% <tt>restart/gs</tt> directory. This number may be different: for example,
1421 !% one could have run previously in <tt>unocc</tt> mode in order to obtain unoccupied
1422 !% Kohn-Sham states, and therefore <tt>restart/gs</tt> will contain more states.
1423 !% These states can be used in the transformation.
1424 !%
1425 !% Note that the code will not check the orthonormality of the new states!
1426 !%
1427 !% Each line provides the coefficients of the new states, in terms of
1428 !% the old ones. The coefficients are complex, but the imaginary part will be
1429 !% ignored for real wavefunctions.
1430 !% Note: This variable cannot be used when parallel in states.
1431 !%End
1432 if (parse_is_defined(namespace, trim(block_name))) then
1433 if (parse_block(namespace, trim(block_name), blk) == 0) then
1434 if (st%parallel_in_states) then
1435 call messages_not_implemented(trim(block_name) // " parallel in states", namespace=namespace)
1436 end if
1437 if (parse_block_n(blk) /= st%nst) then
1438 message(1) = "Number of rows in block " // trim(block_name) // " must equal number of states in this calculation."
1439 call messages_fatal(1, namespace=namespace)
1440 end if
1441 call states_elec_copy(stin, st, exclude_wfns = .true.)
1442 call states_elec_look_and_load(restart, namespace, space, stin, mesh, kpoints)
1443
1444 ! FIXME: rotation matrix should be R_TYPE
1445 safe_allocate(rotation_matrix(1:stin%nst, 1:stin%nst))
1446 safe_allocate(psi(1:mesh%np, 1:st%d%dim))
1447
1448 rotation_matrix = diagonal_matrix(stin%nst, m_z1)
1449
1450 do ist = 1, st%nst
1451 ncols = parse_block_cols(blk, ist-1)
1452 if (ncols /= stin%nst) then
1453 write(message(1),'(a,i6,a,i6,3a,i6,a)') "Number of columns (", ncols, ") in row ", ist, " of block ", &
1454 trim(block_name), " must equal number of states (", stin%nst, ") read from gs restart."
1455 call messages_fatal(1, namespace=namespace)
1456 end if
1457 do jst = 1, stin%nst
1458 call parse_block_cmplx(blk, ist - 1, jst - 1, rotation_matrix(jst, ist))
1459 end do
1460 end do
1461
1462 call parse_block_end(blk)
1463
1464 do iqn = st%d%kpt%start, st%d%kpt%end
1465 if (states_are_real(st)) then
1466 call states_elec_rotate(stin, namespace, mesh, real(rotation_matrix, real64) , iqn)
1467 else
1468 call states_elec_rotate(stin, namespace, mesh, rotation_matrix, iqn)
1469 end if
1470
1471 do ist = st%st_start, st%st_end
1472 call states_elec_get_state(stin, mesh, ist, iqn, psi)
1473 call states_elec_set_state(st, mesh, ist, iqn, psi)
1474 end do
1475
1476 end do
1477
1478 safe_deallocate_a(rotation_matrix)
1479 safe_deallocate_a(psi)
1480
1482
1483 else
1484 call messages_variable_is_block(namespace, trim(block_name))
1485 end if
1486
1487 end if
1488
1489 pop_sub(states_elec_transform)
1490 end subroutine states_elec_transform
1491
1493
1494
1495!! Local Variables:
1496!! mode: f90
1497!! coding: utf-8
1498!! 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:186
This module handles the calculation mode.
integer, parameter, public p_strategy_max
integer, parameter, public p_strategy_domains
parallelization in domains
integer, parameter, public spinors
real(real64), parameter, public m_zero
Definition: global.F90:188
real(real64), parameter, public m_epsilon
Definition: global.F90:204
complex(real64), parameter, public m_z1
Definition: global.F90:199
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.
Definition: math.F90:115
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:118
subroutine, public messages_print_with_emphasis(msg, iunit, namespace)
Definition: messages.F90:920
subroutine, public messages_not_implemented(feature, namespace)
Definition: messages.F90:1113
character(len=512), private msg
Definition: messages.F90:165
subroutine, public messages_variable_is_block(namespace, name)
Definition: messages.F90:1069
subroutine, public messages_warning(no_lines, all_nodes, namespace)
Definition: messages.F90:537
subroutine, public print_date(str)
Definition: messages.F90:1005
subroutine, public messages_new_line()
Definition: messages.F90:1134
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:160
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:414
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:616
logical function mpi_grp_is_root(grp)
Is the current MPI process of grpcomm, root.
Definition: mpi.F90:434
type(mpi_grp_t), public mpi_world
Definition: mpi.F90:270
This module handles the communicators for the various parallelization strategies.
Definition: multicomm.F90:145
this module contains the low-level part of the output system
Definition: output_low.F90:115
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:232
logical function, public parse_is_defined(namespace, name)
Definition: parser.F90:502
integer function, public parse_block(namespace, name, blk, check_varinfo_)
Definition: parser.F90:618
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
Definition: profiling.F90:623
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
Definition: profiling.F90:552
subroutine, public restart_read(restart, iunit, lines, nlines, ierr)
Definition: restart.F90:936
subroutine, public restart_close(restart, iunit)
Close a file previously opened with restart_open.
Definition: restart.F90:953
subroutine, public restart_write(restart, iunit, lines, nlines, ierr)
Definition: restart.F90:909
logical pure function, public restart_skip(restart)
Returns true if the restart information should neither be read nor written. This might happen because...
Definition: restart.F90:970
integer function, public restart_open(restart, filename, status, position, silent)
Open file "filename" found inside the current restart directory. Depending on the type of restart,...
Definition: restart.F90:862
subroutine, public smear_init(this, namespace, ispin, fixed_occ, integral_occs, kpoints)
Definition: smear.F90:185
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_dump(restart, space, st, mesh, kpoints, ierr, iter, lr, st_start_writing, verbose)
subroutine, public states_elec_look_and_load(restart, namespace, space, st, mesh, kpoints, is_complex, packed)
subroutine, public states_elec_transform(st, namespace, space, restart, mesh, kpoints, prefix)
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, public states_elec_dump_spin(restart, st, ierr)
subroutine, public conv_to_c_string(str)
converts to c string
Definition: string.F90:252
type(type_t), public type_float
Definition: types.F90:133
type(type_t), public type_cmplx
Definition: types.F90:134
subroutine fill_random()
logical function index_is_wrong()
Describes mesh distribution to nodes.
Definition: mesh.F90:186
The states_elec_t class contains all electronic wave functions.
int true(void)