Octopus
states_elec.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2006 M. Marques, A. Castro, A. Rubio, G. Bertsch
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#include "global.h"
19
21 use accel_oct_m
22 use batch_oct_m
26 use batch_oct_m
29 use comm_oct_m
30 use debug_oct_m
34 use global_oct_m
35 use grid_oct_m
36 use io_oct_m
37 use, intrinsic :: iso_fortran_env
40 use loct_oct_m
41 use math_oct_m
42 use mesh_oct_m
47 use mpi_oct_m
50#ifdef HAVE_OPENMP
51 use omp_lib
52#endif
53 use parser_oct_m
57 use smear_oct_m
58 use space_oct_m
62 use types_oct_m
63 use unit_oct_m
67
68 implicit none
69
70 private
71
72 public :: &
103 stress_t, &
105
106
107 ! this type must be moved to stress module but due to circular dependency it is not possible now
108 type stress_t
109 real(real64) :: total(3,3) = m_zero
110 real(real64) :: kinetic(3,3) = m_zero
111 real(real64) :: Hartree(3,3) = m_zero
112 real(real64) :: xc(3,3) = m_zero
113 real(real64) :: xc_nlcc(3,3) = m_zero
114 real(real64) :: ps_local(3,3) = m_zero
115 real(real64) :: ps_nl(3,3) = m_zero
116 real(real64) :: ion_ion(3,3) = m_zero
117 real(real64) :: vdw(3,3) = m_zero
118 real(real64) :: hubbard(3,3) = m_zero
119
120 real(real64) :: kinetic_sumrule = m_zero
121 real(real64) :: hartree_sumrule = m_zero
122 end type stress_t
123
124 ! TODO(Alex) Issue #672 Decouple k-point info from `states_elec_dim_t`
125
133 !
134 type, extends(states_abst_t) :: states_elec_t
135 ! Components are public by default
136 type(states_elec_dim_t) :: d
137 integer :: nst_conv
138
139 logical :: only_userdef_istates
140 logical :: requires_generalized_overlap = .false.
141
142 type(states_elec_group_t) :: group
143 integer :: block_size
147 logical :: pack_states
151
152
153 character(len=1024), allocatable :: user_def_states(:,:,:)
156
157 ! TODO(Alex) Issue #821. Collate current quantities into an object.
158 ! the densities and currents (after all we are doing DFT :)
159 real(real64), allocatable :: rho(:,:)
160 real(real64), allocatable :: rho_core(:)
161
162 ! Buffer density
163 type(accel_mem_t) :: buff_density
164
165 real(real64), allocatable :: current(:, :, :)
166 real(real64), allocatable :: current_para(:, :, :)
167 real(real64), allocatable :: current_dia(:, :, :)
168 real(real64), allocatable :: current_mag(:, :, :)
169 real(real64), allocatable :: current_kpt(:,:,:)
170
171 ! TODO(Alex) Issue #673. Create frozen density class and replace in states_elec_t
172 ! It may be required to "freeze" the deepest orbitals during the evolution; the density
173 ! of these orbitals is kept in frozen_rho. It is different from rho_core.
174 real(real64), allocatable :: frozen_rho(:, :)
175 real(real64), allocatable :: frozen_tau(:, :)
176 real(real64), allocatable :: frozen_gdens(:,:,:)
177 real(real64), allocatable :: frozen_ldens(:,:)
178
179 logical :: uniform_occ
180
181 real(real64), allocatable :: eigenval(:,:)
182 logical :: fixed_occ
183 logical :: restart_fixed_occ
184 logical :: restart_reorder_occs
185 real(real64), allocatable :: occ(:,:)
186 real(real64), allocatable :: kweights(:)
187 integer :: nik
188
189 logical :: fixed_spins
191 real(real64), allocatable :: spin(:, :, :)
192
193 real(real64) :: qtot
194 real(real64) :: val_charge
195
196 type(stress_t) :: stress_tensors
197
198 logical :: fromScratch
199 type(smear_t) :: smear
200
201 ! TODO(Alex) Issue #823 Move modelmbparticles out of states_elec_t
202 type(modelmb_particle_t) :: modelmbparticles
204 ! TODO(Alex) Issue #824. Package the communicators in a single instance prior to removing
205 ! or consider creating a distributed_t instance for each (as distributed_t contains the an instance of mpi_grp_t)
206 type(mpi_grp_t) :: mpi_grp
207 type(mpi_grp_t) :: dom_st_mpi_grp
208 type(mpi_grp_t) :: st_kpt_mpi_grp
209 type(mpi_grp_t) :: dom_st_kpt_mpi_grp
210 type(mpi_grp_t) :: system_grp
211 type(blacs_proc_grid_t) :: dom_st_proc_grid
212
213 type(distributed_t) :: dist
214 logical :: scalapack_compatible
215 logical :: parallel_in_states = .false.
216
217 ! TODO(Alex) Issue #820. Remove lnst, st_start, st_end and node, as they are all contained within dist
218 integer :: lnst
219 integer :: st_start, st_end
220 integer, allocatable :: node(:)
221
222 ! TODO(Alex) Issue #824. Either change from data to a method, or package with `st_kpt_mpi_grp`
223 integer, allocatable :: st_kpt_task(:,:)
224
225 type(multicomm_all_pairs_t), private :: ap
226 logical :: symmetrize_density
227 integer :: randomization
228 integer :: orth_method = 0
230 real(real64) :: gpu_states_mem
231
232 contains
233 procedure :: nullify => states_elec_null
234 procedure :: write_info => states_elec_write_info
235 procedure :: pack => states_elec_pack
236 procedure :: unpack => states_elec_unpack
237 procedure :: set_zero => states_elec_set_zero
238 procedure :: dipole => states_elec_calculate_dipole
239 end type states_elec_t
242 integer, public, parameter :: &
243 par_independent = 1, &
244 par_dependent = 2
245
247 interface states_elec_get_state
250 end interface states_elec_get_state
251
255 end interface states_elec_set_state
257 interface states_elec_get_points
263 end interface
264
265contains
266
267 ! TODO(Alex): Issue #826. Rename to something like "states_elec_default_wfs_type", or remove
268 subroutine states_elec_null(st)
269 class(states_elec_t), intent(inout) :: st
271 push_sub(states_elec_null)
273 st%wfs_type = type_float ! By default, calculations use real wavefunctions
275 st%packed = .false.
276 st%requires_generalized_overlap = .false.
279 end subroutine states_elec_null
281
283 subroutine states_elec_init(st, namespace, space, valence_charge, kpoints, calc_mode_id)
284 type(states_elec_t), target, intent(inout) :: st
285 type(namespace_t), intent(in) :: namespace
286 type(electron_space_t), intent(in) :: space
287 real(real64), intent(in) :: valence_charge
288 type(kpoints_t), intent(in) :: kpoints
289 integer, optional, intent(in) :: calc_mode_id
290
291 real(real64) :: excess_charge, nempty_percent
292 integer :: nempty, ntot, default
293 integer :: nempty_conv, nempty_conv_default
294 logical :: force, adapt_for_chebyshev
295 real(real64), parameter :: tol = 1e-13_real64
296 integer :: es
297 integer, parameter :: rs_chebyshev = 12
298 integer(int64), parameter :: chebyshev_compatible_modes(3) = &
299 [option__calculationmode__gs, option__calculationmode__go, option__calculationmode__unocc]
301 push_sub_with_profile(states_elec_init)
303 st%fromScratch = .true. ! this will be reset if restart_read is called
305
306 ! We get the spin dimension from the electronic space
307 ! TODO: Remove spin space information from states_elec_dim
308 st%d%ispin = space%ispin
309
310 ! Use of spinors requires complex wavefunctions.
311 if (st%d%ispin == spinors) call states_set_complex(st)
313 if (st%d%ispin /= unpolarized .and. kpoints%use_time_reversal) then
314 message(1) = "Time reversal symmetry is only implemented for unpolarized spins."
315 message(2) = "Use KPointsUseTimeReversal = no."
316 call messages_fatal(2, namespace=namespace)
317 end if
320 !%Variable ExcessCharge
321 !%Type float
322 !%Default 0.0
323 !%Section States
324 !%Description
325 !% The net charge of the system. A negative value means that we are adding
326 !% electrons, while a positive value means we are taking electrons
327 !% from the system.
328 !%End
329 call parse_variable(namespace, 'ExcessCharge', m_zero, excess_charge)
331 !%Variable TotalStates
332 !%Type integer
333 !%Default 0
334 !%Section States
335 !%Description
336 !% This variable sets the total number of states that Octopus will
337 !% use. This is normally not necessary since by default Octopus
338 !% sets the number of states to the minimum necessary to hold the
339 !% electrons present in the system. (This default behavior is
340 !% obtained by setting <tt>TotalStates</tt> to 0).
341 !%
342 !% If you want to add some unoccupied states, probably it is more convenient to use the variable
343 !% <tt>ExtraStates</tt>.
344 !%End
345 call parse_variable(namespace, 'TotalStates', 0, ntot)
346 if (ntot < 0) then
347 write(message(1), '(a,i5,a)') "Input: '", ntot, "' is not a valid value for TotalStates."
348 call messages_fatal(1, namespace=namespace)
349 end if
351 !%Variable ExtraStates
352 !%Type integer
353 !%Default 0
354 !%Section States
355 !%Description
356 !% The number of states is in principle calculated considering the minimum
357 !% numbers of states necessary to hold the electrons present in the system.
358 !% The number of electrons is
359 !% in turn calculated considering the nature of the species supplied in the
360 !% <tt>Species</tt> block, and the value of the <tt>ExcessCharge</tt> variable.
361 !% However, one may command <tt>Octopus</tt> to use more states, which is necessary if one wants to
362 !% use fractional occupational numbers, either fixed from the beginning through
363 !% the <tt>Occupations</tt> block or by prescribing
364 !% an electronic temperature with <tt>Smearing</tt>, or in order to calculate
365 !% excited states (including with <tt>CalculationMode = unocc</tt>).
366 !%End
367 call parse_variable(namespace, 'ExtraStates', 0, nempty)
368 if (nempty < 0) then
369 write(message(1), '(a,i5,a)') "Input: '", nempty, "' is not a valid value for ExtraStates."
370 message(2) = '(0 <= ExtraStates)'
371 call messages_fatal(2, namespace=namespace)
372 end if
373
374 if (ntot > 0 .and. nempty > 0) then
375 message(1) = 'You cannot set TotalStates and ExtraStates at the same time.'
376 call messages_fatal(1, namespace=namespace)
377 end if
378
379 !%Variable ExtraStatesInPercent
380 !%Type float
381 !%Default 0
382 !%Section States
383 !%Description
384 !% This variable allows to set the number of extra/empty states as percentage of the
385 !% used occupied states. For example, a value 35 for ExtraStatesInPercent would amount
386 !% to ceiling(35/100 * nstates) extra states, where nstates denotes the amount of occupied
387 !% states Octopus is using for the system at hand.
388 !%End
389 call parse_variable(namespace, 'ExtraStatesInPercent', m_zero, nempty_percent)
390 if (nempty_percent < 0) then
391 write(message(1), '(a,f8.6,a)') "Input: '", nempty_percent, &
392 "' should be a percentage value x (where x is parts in hundred) larger or equal 0"
393 call messages_fatal(1, namespace=namespace)
394 end if
395
396 if (nempty > 0 .and. nempty_percent > 0) then
397 message(1) = 'You cannot set ExtraStates and ExtraStatesInPercent at the same time.'
398 call messages_fatal(1, namespace=namespace)
399 end if
400
401 ! Only use extra states for Chebyshev filtering in ground state or geometry optimization runs
402 adapt_for_chebyshev = .false.
403 if (present(calc_mode_id)) then
404 if (any(calc_mode_id == chebyshev_compatible_modes)) then
405 ! this variable is documented in electrons/eigensolver.F90
406 call parse_variable(namespace, 'Eigensolver', rs_chebyshev, es)
407 if (es == rs_chebyshev) then
408 ! reuse this condition below
409 adapt_for_chebyshev = .true.
410 end if
411 end if
412 end if
413 if (adapt_for_chebyshev) then
414 if (nempty == 0 .and. is_close(nempty_percent, m_zero)) then
415 nempty_percent = 15
416 message(1) = 'Chebyshev filtering eigensolver detected. Setting ExtraStatesInPercent = 15'
417 call messages_info(1, namespace=namespace)
418 end if
419 end if
420
421 ! For non-periodic systems this should just return the Gamma point
422 call states_elec_choose_kpoints(st, kpoints, namespace)
423
424 st%val_charge = valence_charge
425
426 st%qtot = -(st%val_charge + excess_charge)
427
428 if (st%qtot < -m_epsilon) then
429 write(message(1),'(a,f12.6,a)') 'Total charge = ', st%qtot, ' < 0'
430 message(2) = 'Check Species and ExcessCharge.'
431 call messages_fatal(2, only_root_writes = .true., namespace=namespace)
432 end if
433
434 select case (st%d%ispin)
435 case (unpolarized)
436 st%d%dim = 1
437 st%nst = nint(st%qtot/2)
438 if (st%nst*2 - st%qtot < -tol) st%nst = st%nst + 1
439 st%d%nspin = 1
440 st%d%spin_channels = 1
441 case (spin_polarized)
442 st%d%dim = 1
443 st%nst = nint(st%qtot/2)
444 if (st%nst*2 - st%qtot < -tol) st%nst = st%nst + 1
445 st%d%nspin = 2
446 st%d%spin_channels = 2
447 case (spinors)
448 st%d%dim = 2
449 st%nst = nint(st%qtot)
450 if (st%nst - st%qtot < -tol) st%nst = st%nst + 1
451 st%d%nspin = 4
452 st%d%spin_channels = 2
453 end select
454
455 if (nempty_percent > 0) then
456 nempty = ceiling(nempty_percent * st%nst / 100)
457 end if
458
459 !%Variable ExtraStatesToConverge
460 !%Type integer
461 !%Default <tt>ExtraStates</tt> (Default 0)
462 !%Section States
463 !%Description
464 !% For <tt>gs</tt> and <tt>unocc</tt> calculations.
465 !% (For the <tt>gs</tt> calculation one needs to set <tt>ConvEigenError=yes</tt>)
466 !% Specifies the number of extra states that will be considered for reaching the convergence.
467 !% The calculation will consider the number off occupied states plus
468 !% <tt>ExtraStatesToConverge</tt> for the convergence criteria.
469 !% By default, all extra states need to be converged (For <tt>gs</tt> calculations only with <tt>ConvEigenError=yes</tt>).
470 !% Thus, together with <tt>ExtraStates</tt>, one can have some more states which will not be
471 !% considered for the convergence criteria, thus making the convergence of the
472 !% unocc calculation faster.
473 !%
474 !% If chebyshev filtering is used, the default is <tt>min(0.8*ExtraStates, ExtraStates - 1)</tt>.
475 !%End
476 nempty_conv_default = nempty
477 if (adapt_for_chebyshev) then
478 ! use 80% of the extra states, but at least do not converge one extra state
479 nempty_conv_default = min(int(0.8*nempty), nempty - 1)
480 end if
481 call parse_variable(namespace, 'ExtraStatesToConverge', nempty_conv_default, nempty_conv)
482 if (nempty_conv < 0) then
483 write(message(1), '(a,i5,a)') "Input: '", nempty_conv, "' is not a valid value for ExtraStatesToConverge."
484 message(2) = '(0 <= ExtraStatesToConverge)'
485 call messages_fatal(2, namespace=namespace)
486 end if
487
488 if (nempty_conv > nempty) then
489 nempty_conv = nempty
490 message(1) = 'You cannot set ExtraStatesToConverge to a higher value than ExtraStates.'
491 message(2) = 'Capping ExtraStatesToConverge to ExtraStates.'
492 call messages_warning(2, namespace=namespace)
493 endif
494
495 if (ntot > 0) then
496 if (ntot < st%nst) then
497 message(1) = 'TotalStates is smaller than the number of states required by the system.'
498 call messages_fatal(1, namespace=namespace)
499 end if
500
501 st%nst = ntot
502 end if
503
504 st%nst_conv = st%nst + nempty_conv
505 st%nst = st%nst + nempty
506 if (st%nst == 0) then
507 message(1) = "Cannot run with number of states = zero."
508 call messages_fatal(1, namespace=namespace)
509 end if
510
511 !%Variable StatesBlockSize
512 !%Type integer
513 !%Section Execution::Optimization
514 !%Description
515 !% Some routines work over blocks of eigenfunctions, which
516 !% generally improves performance at the expense of increased
517 !% memory consumption. This variable selects the size of the
518 !% blocks to be used. If GPUs are used, the default is the
519 !% warp size (32 for NVIDIA, 32 or 64 for AMD);
520 !% otherwise it is 4.
521 !%End
522
523 if (accel_is_enabled()) then
524 ! use the warp size (usually 32, for some AMD GPUs it is 64)
525 default = accel%warp_size
526 else
527 default = 4
528 end if
529
530 if (default > pad_pow2(st%nst)) default = pad_pow2(st%nst)
531
532 assert(default > 0)
533
534 call parse_variable(namespace, 'StatesBlockSize', default, st%block_size)
535 if (st%block_size < 1) then
536 call messages_write("The variable 'StatesBlockSize' must be greater than 0.")
537 call messages_fatal(namespace=namespace)
538 end if
539
540 st%block_size = min(st%block_size, st%nst)
541 conf%target_states_block_size = st%block_size
542
543 safe_allocate(st%eigenval(1:st%nst, 1:st%nik))
544 st%eigenval = huge(st%eigenval)
545
546 ! Periodic systems require complex wavefunctions
547 ! but not if it is Gamma-point only
548 if (.not. kpoints%gamma_only()) then
549 call states_set_complex(st)
550 end if
551
552 !%Variable OnlyUserDefinedInitialStates
553 !%Type logical
554 !%Default no
555 !%Section States
556 !%Description
557 !% If true, then only user-defined states from the block <tt>UserDefinedStates</tt>
558 !% will be used as initial states for a time-propagation. No attempt is made
559 !% to load ground-state orbitals from a previous ground-state run.
560 !%End
561 call parse_variable(namespace, 'OnlyUserDefinedInitialStates', .false., st%only_userdef_istates)
562
563 ! we now allocate some arrays
564 safe_allocate(st%occ (1:st%nst, 1:st%nik))
565 st%occ = m_zero
566 ! allocate space for formula strings that define user-defined states
567 if (parse_is_defined(namespace, 'UserDefinedStates') .or. parse_is_defined(namespace, 'OCTInitialUserdefined') &
568 .or. parse_is_defined(namespace, 'OCTTargetUserdefined')) then
569 safe_allocate(st%user_def_states(1:st%d%dim, 1:st%nst, 1:st%nik))
570 ! initially we mark all 'formulas' as undefined
571 st%user_def_states(1:st%d%dim, 1:st%nst, 1:st%nik) = 'undefined'
572 end if
573
574 if (st%d%ispin == spinors) then
575 safe_allocate(st%spin(1:3, 1:st%nst, 1:st%nik))
576 end if
577
578 !%Variable StatesRandomization
579 !%Type integer
580 !%Default par_independent
581 !%Section States
582 !%Description
583 !% The randomization of states can be done in two ways:
584 !% i) a parallelisation independent way (default), where the random states are identical,
585 !% irrespectively of the number of tasks and
586 !% ii) a parallelisation dependent way, which can prevent linear dependency
587 !% to occur for large systems.
588 !%Option par_independent 1
589 !% Parallelisation-independent randomization of states.
590 !%Option par_dependent 2
591 !% The randomization depends on the number of taks used in the calculation.
592 !%End
593 call parse_variable(namespace, 'StatesRandomization', par_independent, st%randomization)
594
595
596 call states_elec_read_initial_occs(st, namespace, excess_charge, kpoints)
597 call states_elec_read_initial_spins(st, namespace)
598
599 ! This test can only be done here, as smear_init is called inside states_elec_read_initial_occs, and
600 ! only there smear%photodop is set.
601
602 if (st%smear%photodop) then
603 if (nempty == 0) then
604 write(message(1), '(a,i5,a)') "PhotoDoping requires to specify ExtraStates."
605 message(2) = '(0 == ExtraStates)'
606 call messages_fatal(2, namespace=namespace)
607 end if
608 end if
609
610 st%st_start = 1
611 st%st_end = st%nst
612 st%lnst = st%nst
613 safe_allocate(st%node(1:st%nst))
614 st%node(1:st%nst) = 0
615
616 call mpi_grp_init(st%mpi_grp, mpi_comm_undefined)
617 st%parallel_in_states = .false.
618
619 call distributed_init_serial(st%d%kpt, st%nik)
620
621 call modelmb_particles_init(st%modelmbparticles, namespace, space, st%nst)
622
623 !%Variable SymmetrizeDensity
624 !%Type logical
625 !%Default no
626 !%Section States
627 !%Description
628 !% When enabled the density is symmetrized. Currently, this can
629 !% only be done for periodic systems. (Experimental.)
630 !%End
631 call parse_variable(namespace, 'SymmetrizeDensity', kpoints%use_symmetries, st%symmetrize_density)
632 call messages_print_var_value('SymmetrizeDensity', st%symmetrize_density, namespace=namespace)
633
634 !%Variable ForceComplex
635 !%Type logical
636 !%Default no
637 !%Section Execution::Debug
638 !%Description
639 !% Normally <tt>Octopus</tt> determines automatically the type necessary
640 !% for the wavefunctions. When set to yes this variable will
641 !% force the use of complex wavefunctions.
642 !%
643 !% Warning: This variable is designed for testing and
644 !% benchmarking and normal users need not use it.
645 !%End
646 call parse_variable(namespace, 'ForceComplex', .false., force)
647
648 if (force) call states_set_complex(st)
649
650 st%packed = .false.
651
652 pop_sub_with_profile(states_elec_init)
653 end subroutine states_elec_init
654
655 ! ---------------------------------------------------------
658 !
659 subroutine states_elec_look(restart, nik, dim, nst, ierr)
660 class(restart_t), intent(in) :: restart
661 integer, intent(out) :: nik
662 integer, intent(out) :: dim
663 integer, intent(out) :: nst
664 integer, intent(out) :: ierr
665
666 character(len=256) :: lines(3)
667 character(len=20) :: char
668 integer :: iunit
669
670 push_sub(states_elec_look)
671
672 ierr = 0
673
674 iunit = restart%open('states')
675 call restart%read(iunit, lines, 3, ierr)
676 if (ierr == 0) then
677 read(lines(1), *) char, nst
678 read(lines(2), *) char, dim
679 read(lines(3), *) char, nik
680 end if
681 call restart%close(iunit)
682
683 pop_sub(states_elec_look)
684 end subroutine states_elec_look
685
686 ! ---------------------------------------------------------
695 !
696 subroutine states_elec_read_initial_occs(st, namespace, excess_charge, kpoints)
697 type(states_elec_t), intent(inout) :: st
698 type(namespace_t), intent(in) :: namespace
699 real(real64), intent(in) :: excess_charge
700 type(kpoints_t), intent(in) :: kpoints
701
702 integer :: ik, ist, ispin, nspin, ncols, nrows, el_per_state, icol, start_pos, spin_n
703 type(block_t) :: blk
704 real(real64) :: rr, charge
705 logical :: integral_occs, unoccupied_states
706 real(real64), allocatable :: read_occs(:, :)
707 real(real64) :: charge_in_block
708
710
711 !%Variable RestartFixedOccupations
712 !%Type logical
713 !%Section States
714 !%Description
715 !% Setting this variable will make the restart proceed as
716 !% if the occupations from the previous calculation had been set via the <tt>Occupations</tt> block,
717 !% <i>i.e.</i> fixed. Otherwise, occupations will be determined by smearing.
718 !%
719 !% Linear response calculations and self-consistent calculations are unaffected by this variable.
720 !% This is however used for Sternheimer calculations.
721 !%End
722 call parse_variable(namespace, 'RestartFixedOccupations', .true., st%restart_fixed_occ)
723 ! we will turn on st%fixed_occ if restart_read is ever called
724
725 !%Variable Occupations
726 !%Type block
727 !%Section States
728 !%Description
729 !% The occupation numbers of the orbitals can be fixed through the use of this
730 !% variable. For example:
731 !%
732 !% <tt>%Occupations
733 !% <br>&nbsp;&nbsp;2 | 2 | 2 | 2 | 2
734 !% <br>%</tt>
735 !%
736 !% would fix the occupations of the five states to 2. There can be
737 !% at most as many columns as states in the calculation. If there are fewer columns
738 !% than states, then the code will assume that the user is indicating the occupations
739 !% of the uppermost states where all lower states have full occupation (i.e. 2 for spin-unpolarized
740 !% calculations, 1 otherwise) and all higher states have zero occupation. The first column
741 !% will be taken to refer to the lowest state such that the occupations would be consistent
742 !% with the correct total charge. For example, if there are 8 electrons and 10 states (from
743 !% <tt>ExtraStates = 6</tt>), then an abbreviated specification
744 !%
745 !% <tt>%Occupations
746 !% <br>&nbsp;&nbsp;1 | 0 | 1
747 !% <br>%</tt>
748 !%
749 !% would be equivalent to a full specification
750 !%
751 !% <tt>%Occupations
752 !% <br>&nbsp;&nbsp;2 | 2 | 2 | 1 | 0 | 1 | 0 | 0 | 0 | 0
753 !% <br>%</tt>
754 !%
755 !% This is an example of use for constrained density-functional theory,
756 !% crudely emulating a HOMO->LUMO+1 optical excitation.
757 !% The number of rows should be equal
758 !% to the number of k-points times the number of spins. For example, for a finite system
759 !% with <tt>SpinComponents == spin_polarized</tt>,
760 !% this block should contain two lines, one for each spin channel.
761 !% All rows must have the same number of columns.
762 !%
763 !% The <tt>Occupations</tt> block is useful for the ground state of highly symmetric
764 !% small systems (like an open-shell atom), to fix the occupation numbers
765 !% of degenerate states in order to help <tt>octopus</tt> to converge. This is to
766 !% be used in conjuction with <tt>ExtraStates</tt>. For example, to calculate the
767 !% carbon atom, one would do:
768 !%
769 !% <tt>ExtraStates = 2
770 !% <br>%Occupations
771 !% <br>&nbsp;&nbsp;2 | 2/3 | 2/3 | 2/3
772 !% <br>%</tt>
773 !%
774 !% If you want the calculation to be spin-polarized (which makes more sense), you could do:
775 !%
776 !% <tt>ExtraStates = 2
777 !% <br>%Occupations
778 !% <br>&nbsp;&nbsp; 2/3 | 2/3 | 2/3
779 !% <br>&nbsp;&nbsp; 0 | 0 | 0
780 !% <br>%</tt>
781 !%
782 !% Note that in this case the first state is absent, the code will calculate four states
783 !% (two because there are four electrons, plus two because <tt>ExtraStates</tt> = 2), and since
784 !% it finds only three columns, it will occupy the first state with one electron for each
785 !% of the spin options.
786 !%
787 !% If the sum of occupations is not equal to the total charge set by <tt>ExcessCharge</tt>,
788 !% an error message is printed.
789 !% If <tt>FromScratch = no</tt> and <tt>RestartFixedOccupations = yes</tt>,
790 !% this block will be ignored.
791 !%End
792
793 integral_occs = .true.
794
795 occ_fix: if (parse_block(namespace, 'Occupations', blk) == 0) then
796 ! read in occupations
797 st%fixed_occ = .true.
798
799 ncols = parse_block_cols(blk, 0)
800 if (ncols > st%nst) then
801 call messages_input_error(namespace, "Occupations", "Too many columns in block Occupations.")
802 end if
803
804 nrows = parse_block_n(blk)
805 if (nrows /= st%nik) then
806 call messages_input_error(namespace, "Occupations", "Wrong number of rows in block Occupations.")
807 end if
808
809 do ik = 1, st%nik - 1
810 if (parse_block_cols(blk, ik) /= ncols) then
811 call messages_input_error(namespace, "Occupations", &
812 "All rows in block Occupations must have the same number of columns.")
813 end if
814 end do
815
816 ! Now we fill all the "missing" states with the maximum occupation.
817 if (st%d%ispin == unpolarized) then
818 el_per_state = 2
819 else
820 el_per_state = 1
821 end if
822
823 safe_allocate(read_occs(1:ncols, 1:st%nik))
824
825 charge_in_block = m_zero
826 do ik = 1, st%nik
827 do icol = 1, ncols
828 call parse_block_float(blk, ik - 1, icol - 1, read_occs(icol, ik))
829 charge_in_block = charge_in_block + read_occs(icol, ik) * st%kweights(ik)
830 end do
831 end do
832
833 spin_n = 2
834 select case (st%d%ispin)
835 case (unpolarized)
836 spin_n = 2
837 case (spin_polarized)
838 spin_n = 2
839 case (spinors)
840 spin_n = 1
841 end select
842
843 start_pos = nint((st%qtot - charge_in_block)/spin_n)
844
845 if (start_pos + ncols > st%nst) then
846 message(1) = "To balance charge, the first column in block Occupations is taken to refer to state"
847 write(message(2),'(a,i6,a)') "number ", start_pos, " but there are too many columns for the number of states."
848 write(message(3),'(a,i6,a)') "Solution: set ExtraStates = ", start_pos + ncols - st%nst
849 call messages_fatal(3, namespace=namespace)
850 end if
851
852 do ik = 1, st%nik
853 do ist = 1, start_pos
854 st%occ(ist, ik) = el_per_state
855 end do
856 end do
857
858 do ik = 1, st%nik
859 do ist = start_pos + 1, start_pos + ncols
860 st%occ(ist, ik) = read_occs(ist - start_pos, ik)
861 integral_occs = integral_occs .and. &
862 abs((st%occ(ist, ik) - el_per_state) * st%occ(ist, ik)) <= m_epsilon
863 end do
864 end do
865
866 do ik = 1, st%nik
867 do ist = start_pos + ncols + 1, st%nst
868 st%occ(ist, ik) = m_zero
869 end do
870 end do
871
872 call parse_block_end(blk)
873
874 safe_deallocate_a(read_occs)
875
876 else
877 st%fixed_occ = .false.
878 integral_occs = .false.
879
880 ! first guess for occupation...paramagnetic configuration
881 rr = m_one
882 if (st%d%ispin == unpolarized) rr = m_two
883
884 st%occ = m_zero
885 st%qtot = -(st%val_charge + excess_charge)
886
887 nspin = 1
888 if (st%d%nspin == 2) nspin = 2
889
890 do ik = 1, st%nik, nspin
891 charge = m_zero
892 do ist = 1, st%nst
893 do ispin = ik, ik + nspin - 1
894 st%occ(ist, ispin) = min(rr, -(st%val_charge + excess_charge) - charge)
895 charge = charge + st%occ(ist, ispin)
896 end do
897 end do
898 end do
899
900 end if occ_fix
901
902 !%Variable RestartReorderOccs
903 !%Type logical
904 !%Default no
905 !%Section States
906 !%Description
907 !% Consider doing a ground-state calculation, and then restarting with new occupations set
908 !% with the <tt>Occupations</tt> block, in an attempt to populate the orbitals of the original
909 !% calculation. However, the eigenvalues may reorder as the density changes, in which case the
910 !% occupations will now be referring to different orbitals. Setting this variable to yes will
911 !% try to solve this issue when the restart data is being read, by reordering the occupations
912 !% according to the order of the expectation values of the restart wavefunctions.
913 !%End
914 if (st%fixed_occ) then
915 call parse_variable(namespace, 'RestartReorderOccs', .false., st%restart_reorder_occs)
916 else
917 st%restart_reorder_occs = .false.
918 end if
919
920 call smear_init(st%smear, namespace, st%d%ispin, st%fixed_occ, integral_occs, kpoints)
921
922 unoccupied_states = (st%d%ispin /= spinors .and. st%nst*2 > st%qtot) .or. (st%d%ispin == spinors .and. st%nst > st%qtot)
923
924 if (.not. smear_is_semiconducting(st%smear) .and. .not. st%smear%method == smear_fixed_occ) then
925 if (.not. unoccupied_states) then
926 call messages_write('Smearing needs unoccupied states (via ExtraStates or TotalStates) to be useful.')
927 call messages_warning(namespace=namespace)
928 end if
929 end if
930
931 ! sanity check
932 charge = m_zero
933 do ist = 1, st%nst
934 charge = charge + sum(st%occ(ist, 1:st%nik) * st%kweights(1:st%nik))
935 end do
936 if (abs(charge - st%qtot) > 1e-6_real64) then
937 message(1) = "Initial occupations do not integrate to total charge."
938 write(message(2), '(6x,f12.6,a,f12.6)') charge, ' != ', st%qtot
939 call messages_fatal(2, only_root_writes = .true., namespace=namespace)
940 end if
941
942 st%uniform_occ = smear_is_semiconducting(st%smear) .and. .not. unoccupied_states
943
945 end subroutine states_elec_read_initial_occs
946
947
948 ! ---------------------------------------------------------
955 !
956 subroutine states_elec_read_initial_spins(st, namespace)
957 type(states_elec_t), intent(inout) :: st
958 type(namespace_t), intent(in) :: namespace
959
960 integer :: i, j, nrows
961 type(block_t) :: blk
962
964
965 st%fixed_spins = .false.
966 if (st%d%ispin /= spinors) then
968 return
969 end if
970
971 !%Variable InitialSpins
972 !%Type block
973 !%Section States
974 !%Description
975 !% The spin character of the initial random guesses for the spinors can
976 !% be fixed by making use of this block. Note that this will not "fix" the
977 !% the spins during the calculation (this cannot be done in spinors mode, in
978 !% being able to change the spins is why the spinors mode exists in the first
979 !% place).
980 !%
981 !% This block is meaningless and ignored if the run is not in spinors mode
982 !% (<tt>SpinComponents = spinors</tt>).
983 !%
984 !% The structure of the block is very simple: each column contains the desired
985 !% <math>\left< S_x \right>, \left< S_y \right>, \left< S_z \right> </math> for each spinor.
986 !% If the calculation is for a periodic system
987 !% and there is more than one <i>k</i>-point, the spins of all the <i>k</i>-points are
988 !% the same.
989 !%
990 !% For example, if we have two spinors, and we want one in the <math>S_x</math> "down" state,
991 !% and another one in the <math>S_x</math> "up" state:
992 !%
993 !% <tt>%InitialSpins
994 !% <br>&nbsp;&nbsp;&nbsp; 0.5 | 0.0 | 0.0
995 !% <br>&nbsp;&nbsp; -0.5 | 0.0 | 0.0
996 !% <br>%</tt>
997 !%
998 !% WARNING: if the calculation is for a system described by pseudopotentials (as
999 !% opposed to user-defined potentials or model systems), this option is
1000 !% meaningless since the random spinors are overwritten by the atomic orbitals.
1001 !%
1002 !% This constraint must be fulfilled:
1003 !% <br><math> \left< S_x \right>^2 + \left< S_y \right>^2 + \left< S_z \right>^2 = \frac{1}{4} </math>
1004 !%End
1005 spin_fix: if (parse_block(namespace, 'InitialSpins', blk) == 0) then
1006 nrows = parse_block_n(blk)
1007 if (nrows < st%nst) then
1008 message(1) = "Please specify one row for each state in InitialSpins, also for extra states."
1009 call messages_fatal(1, namespace=namespace)
1010 end if
1011 do i = 1, st%nst
1012 do j = 1, 3
1013 call parse_block_float(blk, i-1, j-1, st%spin(j, i, 1))
1014 end do
1015 if (abs(sum(st%spin(1:3, i, 1)**2) - m_fourth) > 1.0e-6_real64) call messages_input_error(namespace, 'InitialSpins')
1016 end do
1017 call parse_block_end(blk)
1018 st%fixed_spins = .true.
1019 do i = 2, st%nik
1020 st%spin(:, :, i) = st%spin(:, :, 1)
1021 end do
1022 end if spin_fix
1023
1025 end subroutine states_elec_read_initial_spins
1026
1027
1028 ! ---------------------------------------------------------
1030 !
1031 subroutine states_elec_allocate_wfns(st, mesh, wfs_type, skip, packed)
1032 type(states_elec_t), intent(inout) :: st
1033 class(mesh_t), intent(in) :: mesh
1034 type(type_t), optional, intent(in) :: wfs_type
1035 logical, contiguous, optional, intent(in) :: skip(:)
1036 logical, optional, intent(in) :: packed
1037
1039
1040 if (present(wfs_type)) then
1041 assert(wfs_type == type_float .or. wfs_type == type_cmplx)
1042 st%wfs_type = wfs_type
1043 end if
1044
1045 call states_elec_init_block(st, mesh, skip = skip, packed=packed)
1046 call states_elec_set_zero(st)
1047
1050
1051 !---------------------------------------------------------------------
1068 subroutine states_elec_init_block(st, mesh, verbose, skip, packed)
1069 type(states_elec_t), intent(inout) :: st
1070 type(mesh_t), intent(in) :: mesh
1071 logical, optional, intent(in) :: verbose
1072 logical, contiguous, optional, intent(in) :: skip(:)
1073 logical, optional, intent(in) :: packed
1074
1075 integer :: ib, iqn, ist, istmin, istmax
1076 logical :: same_node, verbose_, packed_
1077 integer, allocatable :: bstart(:), bend(:)
1078
1079 push_sub(states_elec_init_block)
1080
1081 safe_allocate(bstart(1:st%nst))
1082 safe_allocate(bend(1:st%nst))
1083 safe_allocate(st%group%iblock(1:st%nst))
1084
1085 st%group%iblock = 0
1086
1087 verbose_ = optional_default(verbose, .true.)
1088 packed_ = optional_default(packed, .false.)
1089
1090 !In case we have a list of state to skip, we do not allocate them
1091 istmin = 1
1092 if (present(skip)) then
1093 do ist = 1, st%nst
1094 if (.not. skip(ist)) then
1095 istmin = ist
1096 exit
1097 end if
1098 end do
1099 end if
1100
1101 istmax = st%nst
1102 if (present(skip)) then
1103 do ist = st%nst, istmin, -1
1104 if (.not. skip(ist)) then
1105 istmax = ist
1106 exit
1107 end if
1108 end do
1109 end if
1110
1111 if (present(skip) .and. verbose_) then
1112 call messages_write('Info: Allocating states from ')
1113 call messages_write(istmin, fmt = 'i8')
1114 call messages_write(' to ')
1115 call messages_write(istmax, fmt = 'i8')
1116 call messages_info()
1117 end if
1118
1119 ! count and assign blocks
1120 ib = 0
1121 st%group%nblocks = 0
1122 bstart(1) = istmin
1123 do ist = istmin, istmax
1124 ib = ib + 1
1125
1126 st%group%iblock(ist) = st%group%nblocks + 1
1127
1128 same_node = .true.
1129 if (st%parallel_in_states .and. ist /= istmax) then
1130 ! We have to avoid that states that are in different nodes end
1131 ! up in the same block
1132 same_node = (st%node(ist + 1) == st%node(ist))
1133 end if
1134
1135 if (ib == st%block_size .or. ist == istmax .or. .not. same_node) then
1136 ib = 0
1137 st%group%nblocks = st%group%nblocks + 1
1138 bend(st%group%nblocks) = ist
1139 if (ist /= istmax) bstart(st%group%nblocks + 1) = ist + 1
1140 end if
1141 end do
1142
1143 safe_allocate(st%group%psib(1:st%group%nblocks, st%d%kpt%start:st%d%kpt%end))
1144
1145 safe_allocate(st%group%block_is_local(1:st%group%nblocks, 1:st%nik))
1146 st%group%block_is_local = .false.
1147 st%group%block_start = -1
1148 st%group%block_end = -2 ! this will make that loops block_start:block_end do not run if not initialized
1149
1150 do ib = 1, st%group%nblocks
1151 if (bstart(ib) >= st%st_start .and. bend(ib) <= st%st_end) then
1152 if (st%group%block_start == -1) st%group%block_start = ib
1153 st%group%block_end = ib
1154 do iqn = st%d%kpt%start, st%d%kpt%end
1155 st%group%block_is_local(ib, iqn) = .true.
1156
1157 if (states_are_real(st)) then
1158 call dwfs_elec_init(st%group%psib(ib, iqn), st%d%dim, bstart(ib), bend(ib), mesh%np_part, iqn, &
1159 special=.true., packed=packed_)
1160 else
1161 call zwfs_elec_init(st%group%psib(ib, iqn), st%d%dim, bstart(ib), bend(ib), mesh%np_part, iqn, &
1162 special=.true., packed=packed_)
1163 end if
1164
1165 end do
1166 end if
1167 end do
1168
1169 safe_allocate(st%group%block_range(1:st%group%nblocks, 1:2))
1170 safe_allocate(st%group%block_size(1:st%group%nblocks))
1171
1172 st%group%block_range(1:st%group%nblocks, 1) = bstart(1:st%group%nblocks)
1173 st%group%block_range(1:st%group%nblocks, 2) = bend(1:st%group%nblocks)
1174 st%group%block_size(1:st%group%nblocks) = bend(1:st%group%nblocks) - bstart(1:st%group%nblocks) + 1
1175
1176 st%group%block_initialized = .true.
1177
1178 safe_allocate(st%group%block_node(1:st%group%nblocks, 1:st%nik))
1179 st%group%block_node = 0
1180
1181 assert(allocated(st%node))
1182 assert(all(st%node >= 0) .and. all(st%node < st%mpi_grp%size))
1183
1184 do iqn = st%d%kpt%start, st%d%kpt%end
1185 do ib = st%group%block_start, st%group%block_end
1186 st%group%block_node(ib, iqn) = st%st_kpt_mpi_grp%rank
1187 end do
1188 end do
1189
1190 call comm_allreduce(st%st_kpt_mpi_grp, st%group%block_node)
1191
1192 if (verbose_) then
1193 call messages_write('Info: Blocks of states')
1194 call messages_info()
1195 do ib = 1, st%group%nblocks
1196 call messages_write(' Block ')
1197 call messages_write(ib, fmt = 'i8')
1198 call messages_write(' contains ')
1199 call messages_write(st%group%block_size(ib), fmt = 'i8')
1200 call messages_write(' states')
1201 if (st%group%block_size(ib) > 0) then
1202 call messages_write(':')
1203 call messages_write(st%group%block_range(ib, 1), fmt = 'i8')
1204 call messages_write(' - ')
1205 call messages_write(st%group%block_range(ib, 2), fmt = 'i8')
1206 end if
1207 call messages_info()
1208 end do
1209 end if
1210
1211!!$!!!!DEBUG
1212!!$ ! some debug output that I will keep here for the moment
1213!!$ if (st%system_grp%is_root()) then
1214!!$ print*, "NST ", st%nst
1215!!$ print*, "BLOCKSIZE ", st%block_size
1216!!$ print*, "NBLOCKS ", st%group%nblocks
1217!!$
1218!!$ print*, "==============="
1219!!$ do ist = 1, st%nst
1220!!$ print*, st%node(ist), ist, st%group%iblock(ist)
1221!!$ end do
1222!!$ print*, "==============="
1223!!$
1224!!$ do ib = 1, st%group%nblocks
1225!!$ print*, ib, bstart(ib), bend(ib)
1226!!$ end do
1227!!$
1228!!$ end if
1229!!$!!!!ENDOFDEBUG
1230
1231 safe_deallocate_a(bstart)
1232 safe_deallocate_a(bend)
1233 pop_sub(states_elec_init_block)
1234 end subroutine states_elec_init_block
1235
1236
1237 ! ---------------------------------------------------------
1239 subroutine states_elec_deallocate_wfns(st)
1240 type(states_elec_t), intent(inout) :: st
1241
1243
1244 call states_elec_group_end(st%group, st%d)
1245 ! The wave functions (and their device buffers) are gone, so the batches
1246 ! are no longer packed. Reset the flag so a subsequent st%pack() actually
1247 ! repacks instead of returning early on a stale value.
1248 st%packed = .false.
1249
1251 end subroutine states_elec_deallocate_wfns
1252
1253
1254 ! ---------------------------------------------------------
1255 subroutine states_elec_densities_init(st, gr)
1256 type(states_elec_t), target, intent(inout) :: st
1257 type(grid_t), intent(in) :: gr
1258
1259 real(real64) :: fsize
1260
1262
1263 safe_allocate(st%rho(1:gr%np_part, 1:st%d%nspin))
1264 st%rho = m_zero
1265
1266 fsize = gr%np_part*8.0_real64*st%block_size
1267
1268 call messages_write('Info: states-block size = ')
1269 call messages_write(fsize, fmt = '(f10.1)', align_left = .true., units = unit_megabytes, print_units = .true.)
1270 call messages_info()
1271
1273 end subroutine states_elec_densities_init
1274
1275 !---------------------------------------------------------------------
1276 subroutine states_elec_allocate_current(st, space, mesh)
1277 type(states_elec_t), intent(inout) :: st
1278 class(space_t), intent(in) :: space
1279 class(mesh_t), intent(in) :: mesh
1280
1282
1283 if (.not. allocated(st%current)) then
1284 safe_allocate(st%current(1:mesh%np_part, 1:space%dim, 1:st%d%nspin))
1285 st%current = m_zero
1286 end if
1287
1288 if (.not. allocated(st%current_para)) then
1289 safe_allocate(st%current_para(1:mesh%np_part, 1:space%dim, 1:st%d%nspin))
1290 st%current_para = m_zero
1291 end if
1292
1293 if (.not. allocated(st%current_dia)) then
1294 safe_allocate(st%current_dia(1:mesh%np_part, 1:space%dim, 1:st%d%nspin))
1295 st%current_dia= m_zero
1296 end if
1297
1298 if (.not. allocated(st%current_mag)) then
1299 safe_allocate(st%current_mag(1:mesh%np_part, 1:space%dim, 1:st%d%nspin))
1300 st%current_mag= m_zero
1301 end if
1302
1303 if (.not. allocated(st%current_kpt)) then
1304 safe_allocate(st%current_kpt(1:mesh%np,1:space%dim,st%d%kpt%start:st%d%kpt%end))
1305 st%current_kpt = m_zero
1306 end if
1307
1309 end subroutine states_elec_allocate_current
1310
1311 !---------------------------------------------------------------------
1319 subroutine states_elec_exec_init(st, namespace, mc)
1320 type(states_elec_t), intent(inout) :: st
1321 type(namespace_t), intent(in) :: namespace
1322 type(multicomm_t), intent(in) :: mc
1323
1324 integer :: default
1325
1326 push_sub(states_elec_exec_init)
1327
1328 !%Variable StatesPack
1329 !%Type logical
1330 !%Section Execution::Optimization
1331 !%Description
1332 !% When set to yes, states are stored in packed mode, which improves
1333 !% performance considerably. Not all parts of the code will profit from
1334 !% this, but should nevertheless work regardless of how the states are
1335 !% stored.
1336 !%
1337 !% If GPUs are used and this variable is set to yes, Octopus
1338 !% will store the wave-functions in device (GPU) memory. If
1339 !% there is not enough memory to store all the wave-functions,
1340 !% execution will stop with an error.
1341 !%
1342 !% See also the related <tt>HamiltonianApplyPacked</tt> variable.
1343 !%
1344 !% The default is yes.
1345 !%End
1346
1347 call parse_variable(namespace, 'StatesPack', .true., st%pack_states)
1349 call messages_print_var_value('StatesPack', st%pack_states, namespace=namespace)
1350
1351 call messages_obsolete_variable(namespace, 'StatesMirror')
1352
1353 !%Variable StatesOrthogonalization
1354 !%Type integer
1355 !%Section SCF::Eigensolver
1356 !%Description
1357 !% The full orthogonalization method used by some
1358 !% eigensolvers. The default is <tt>cholesky_serial</tt>, except with state
1359 !% parallelization, the default is <tt>cholesky_parallel</tt>.
1360 !%Option cholesky_serial 1
1361 !% Cholesky decomposition implemented using
1362 !% BLAS/LAPACK. Can be used with domain parallelization but not
1363 !% state parallelization.
1364 !%Option cholesky_parallel 2
1365 !% Cholesky decomposition implemented using
1366 !% ScaLAPACK. Compatible with states parallelization.
1367 !%Option cgs 3
1368 !% Classical Gram-Schmidt (CGS) orthogonalization.
1369 !% Can be used with domain parallelization but not state parallelization.
1370 !% The algorithm is defined in Giraud et al., Computers and Mathematics with Applications 50, 1069 (2005).
1371 !%Option mgs 4
1372 !% Modified Gram-Schmidt (MGS) orthogonalization.
1373 !% Can be used with domain parallelization but not state parallelization.
1374 !% The algorithm is defined in Giraud et al., Computers and Mathematics with Applications 50, 1069 (2005).
1375 !%Option drcgs 5
1376 !% Classical Gram-Schmidt orthogonalization with double-step reorthogonalization.
1377 !% Can be used with domain parallelization but not state parallelization.
1378 !% The algorithm is taken from Giraud et al., Computers and Mathematics with Applications 50, 1069 (2005).
1379 !% According to this reference, this is much more precise than CGS or MGS algorithms.
1380 !% The MGS version seems not to improve much the stability and would require more communications over the domains.
1381 !%End
1382
1383 default = option__statesorthogonalization__cholesky_serial
1384#ifdef HAVE_SCALAPACK
1386 default = option__statesorthogonalization__cholesky_parallel
1387 end if
1388#endif
1389
1390 call parse_variable(namespace, 'StatesOrthogonalization', default, st%orth_method)
1391
1392 if (.not. varinfo_valid_option('StatesOrthogonalization', st%orth_method)) then
1393 call messages_input_error(namespace, 'StatesOrthogonalization')
1394 end if
1395 call messages_print_var_option('StatesOrthogonalization', st%orth_method, namespace=namespace)
1396
1397 !%Variable StatesDeviceMemory
1398 !%Type float
1399 !%Section Execution::Optimization
1400 !%Default -512
1401 !%Description
1402 !% This variable selects the amount of device memory that
1403 !% will be used by Octopus to store the states.
1404 !%
1405 !% A positive number smaller than 1 indicates a fraction of the total
1406 !% device memory. A number larger than one indicates an absolute
1407 !% amount of memory in megabytes. A negative number indicates an
1408 !% amount of memory in megabytes that would be subtracted from
1409 !% the total device memory.
1410 !%End
1411 call parse_variable(namespace, 'StatesDeviceMemory', -512.0_real64, st%gpu_states_mem)
1412 call messages_obsolete_variable(namespace, 'StatesCLDeviceMemory', 'StatesDeviceMemory')
1413
1414 pop_sub(states_elec_exec_init)
1415 end subroutine states_elec_exec_init
1416
1417
1418 ! ---------------------------------------------------------
1420 !
1421 subroutine states_elec_copy(stout, stin, exclude_wfns, exclude_eigenval, special, dest_type, copy_data)
1422 type(states_elec_t), target, intent(inout) :: stout
1423 type(states_elec_t), intent(in) :: stin
1424 logical, optional, intent(in) :: exclude_wfns
1425 logical, optional, intent(in) :: exclude_eigenval
1426 logical, optional, intent(in) :: special
1427 type(type_t), optional, intent(in) :: dest_type
1429 logical, optional, intent(in) :: copy_data
1432
1433 logical :: exclude_wfns_
1434
1435 push_sub(states_elec_copy)
1436
1437 exclude_wfns_ = optional_default(exclude_wfns, .false.)
1438
1439 call states_elec_null(stout)
1440
1441 call states_elec_dim_copy(stout%d, stin%d)
1442 safe_allocate_source_a(stout%kweights, stin%kweights)
1443 stout%nik = stin%nik
1444
1445 call modelmb_particles_copy(stout%modelmbparticles, stin%modelmbparticles)
1446
1447 stout%wfs_type = stin%wfs_type
1448 if (present(dest_type)) then
1449 stout%wfs_type = dest_type
1450 end if
1451 stout%nst = stin%nst
1452 stout%block_size = stin%block_size
1453 stout%orth_method = stin%orth_method
1454
1455 stout%gpu_states_mem = stin%gpu_states_mem
1456 stout%pack_states = stin%pack_states
1457
1458 stout%only_userdef_istates = stin%only_userdef_istates
1459 stout%requires_generalized_overlap = stin%requires_generalized_overlap
1460
1461 if (.not. exclude_wfns_) then
1462 safe_allocate_source_a(stout%rho, stin%rho)
1463 end if
1464
1465 stout%uniform_occ = stin%uniform_occ
1466
1467 if (.not. optional_default(exclude_eigenval, .false.)) then
1468 safe_allocate_source_a(stout%eigenval, stin%eigenval)
1469 safe_allocate_source_a(stout%occ, stin%occ)
1470 safe_allocate_source_a(stout%spin, stin%spin)
1471 end if
1472
1473 ! the call to init_block is done at the end of this subroutine
1474 ! it allocates iblock, psib, block_is_local
1475 stout%group%nblocks = stin%group%nblocks
1476
1477 safe_allocate_source_a(stout%user_def_states, stin%user_def_states)
1478
1479 safe_allocate_source_a(stout%current, stin%current)
1480 safe_allocate_source_a(stout%current_kpt, stin%current_kpt)
1481 safe_allocate_source_a(stout%rho_core, stin%rho_core)
1482 safe_allocate_source_a(stout%frozen_rho, stin%frozen_rho)
1483 safe_allocate_source_a(stout%frozen_tau, stin%frozen_tau)
1484 safe_allocate_source_a(stout%frozen_gdens, stin%frozen_gdens)
1485 safe_allocate_source_a(stout%frozen_ldens, stin%frozen_ldens)
1486
1487 stout%fixed_occ = stin%fixed_occ
1488 stout%restart_fixed_occ = stin%restart_fixed_occ
1489
1490 stout%fixed_spins = stin%fixed_spins
1491
1492 stout%qtot = stin%qtot
1493 stout%val_charge = stin%val_charge
1494
1495 call smear_copy(stout%smear, stin%smear)
1496
1497 stout%parallel_in_states = stin%parallel_in_states
1498 call mpi_grp_copy(stout%mpi_grp, stin%mpi_grp)
1499 call mpi_grp_copy(stout%system_grp, stin%system_grp)
1500 call mpi_grp_copy(stout%dom_st_kpt_mpi_grp, stin%dom_st_kpt_mpi_grp)
1501 call mpi_grp_copy(stout%st_kpt_mpi_grp, stin%st_kpt_mpi_grp)
1502 call mpi_grp_copy(stout%dom_st_mpi_grp, stin%dom_st_mpi_grp)
1503 safe_allocate_source_a(stout%node, stin%node)
1504 safe_allocate_source_a(stout%st_kpt_task, stin%st_kpt_task)
1505
1506#ifdef HAVE_SCALAPACK
1507 call blacs_proc_grid_copy(stin%dom_st_proc_grid, stout%dom_st_proc_grid)
1508#endif
1509
1510 call distributed_copy(stin%dist, stout%dist)
1511
1512 stout%scalapack_compatible = stin%scalapack_compatible
1513
1514 stout%lnst = stin%lnst
1515 stout%st_start = stin%st_start
1516 stout%st_end = stin%st_end
1517
1518 if (stin%parallel_in_states) call multicomm_all_pairs_copy(stout%ap, stin%ap)
1519
1520 stout%symmetrize_density = stin%symmetrize_density
1521
1522 if (.not. exclude_wfns_) call states_elec_group_copy(stin%d, stin%group, stout%group, &
1523 copy_data=copy_data, special=special, dest_type=dest_type)
1524
1525 stout%packed = stin%packed
1526
1527 stout%randomization = stin%randomization
1528
1529 pop_sub(states_elec_copy)
1530 end subroutine states_elec_copy
1531
1532
1533 ! ---------------------------------------------------------
1535 !
1536 subroutine states_elec_end(st)
1537 type(states_elec_t), intent(inout) :: st
1538
1539 push_sub(states_elec_end)
1540
1541 call states_elec_dim_end(st%d)
1542
1543 call modelmb_particles_end(st%modelmbparticles)
1544
1545 ! this deallocates dpsi, zpsi, psib, iblock, iblock
1547
1548 safe_deallocate_a(st%user_def_states)
1549
1550 safe_deallocate_a(st%rho)
1551 safe_deallocate_a(st%eigenval)
1552
1553 safe_deallocate_a(st%current)
1554 safe_deallocate_a(st%current_para)
1555 safe_deallocate_a(st%current_dia)
1556 safe_deallocate_a(st%current_mag)
1557 safe_deallocate_a(st%current_kpt)
1558 safe_deallocate_a(st%rho_core)
1559 safe_deallocate_a(st%frozen_rho)
1560 safe_deallocate_a(st%frozen_tau)
1561 safe_deallocate_a(st%frozen_gdens)
1562 safe_deallocate_a(st%frozen_ldens)
1563 safe_deallocate_a(st%occ)
1564 safe_deallocate_a(st%spin)
1565 safe_deallocate_a(st%kweights)
1566
1567
1568#ifdef HAVE_SCALAPACK
1569 call blacs_proc_grid_end(st%dom_st_proc_grid)
1570#endif
1571
1572 call distributed_end(st%dist)
1573
1574 safe_deallocate_a(st%node)
1575 safe_deallocate_a(st%st_kpt_task)
1576
1577 if (st%parallel_in_states) then
1578 safe_deallocate_a(st%ap%schedule)
1579 end if
1580
1581 if (st%buff_density%allocated) call accel_free_buffer(st%buff_density)
1582
1583 pop_sub(states_elec_end)
1584 end subroutine states_elec_end
1585
1586
1587 ! TODO(Alex) Issue #684. Abstract duplicate code in states_elec_generate_random, to get it to
1588 ! a point where it can be refactored.
1590 subroutine states_elec_generate_random(st, mesh, kpoints, ist_start_, ist_end_, ikpt_start_, ikpt_end_, normalized)
1591 type(states_elec_t), intent(inout) :: st
1592 class(mesh_t), intent(in) :: mesh
1593 type(kpoints_t), intent(in) :: kpoints
1594 integer, optional, intent(in) :: ist_start_
1595 integer, optional, intent(in) :: ist_end_
1596 integer, optional, intent(in) :: ikpt_start_
1597 integer, optional, intent(in) :: ikpt_end_
1598 logical, optional, intent(in) :: normalized
1600
1601 integer :: ist, ik, id, ist_start, ist_end, jst, ikpt_start, ikpt_end
1602 complex(real64) :: alpha, beta, zdotp_tmp
1603 real(real64), allocatable :: dpsi(:, :)
1604 complex(real64), allocatable :: zpsi(:, :), zpsi2(:)
1605 integer :: ikpoint, ip
1606 type(batch_t) :: ffb
1607
1608 logical :: normalized_
1609
1610 normalized_ = optional_default(normalized, .true.)
1611
1613
1614 ist_start = optional_default(ist_start_, 1)
1615 ist_end = optional_default(ist_end_, st%nst)
1616 ikpt_start = optional_default(ikpt_start_, 1)
1617 ikpt_end = optional_default(ikpt_end_, st%nik)
1618
1619 safe_allocate(dpsi(1:mesh%np, 1:st%d%dim))
1620 if (states_are_complex(st)) then
1621 safe_allocate(zpsi(1:mesh%np, 1:st%d%dim))
1622 end if
1623
1624 select case (st%d%ispin)
1626
1627 do ik = ikpt_start, ikpt_end
1628 ikpoint = st%d%get_kpoint_index(ik)
1629 do ist = ist_start, ist_end
1630 if (states_are_real(st).or.kpoints_point_is_gamma(kpoints, ikpoint)) then
1631 if (st%randomization == par_independent) then
1632 call dmf_random(mesh, dpsi(:, 1), &
1633 pre_shift = mesh%pv%xlocal-1, &
1634 post_shift = mesh%pv%np_global - mesh%pv%xlocal - mesh%np + 1, &
1635 normalized = normalized)
1636 ! Ensures that the grid points are properly distributed in the domain parallel case
1637 if(mesh%parallel_in_domains) then
1638 call batch_init(ffb, dpsi(:,1))
1639 call dmesh_batch_exchange_points(mesh, ffb, backward_map = .true.)
1640 call ffb%end()
1641 end if
1642 else
1643 call dmf_random(mesh, dpsi(:, 1), normalized = normalized)
1644 end if
1645 if (.not. state_kpt_is_local(st, ist, ik)) cycle
1646 if (states_are_complex(st)) then !Gamma point
1647 do ip = 1, mesh%np
1648 zpsi(ip,1) = cmplx(dpsi(ip,1), m_zero, real64)
1649 end do
1650 call states_elec_set_state(st, mesh, ist, ik, zpsi)
1651 else
1652 call states_elec_set_state(st, mesh, ist, ik, dpsi)
1653 end if
1654 else
1655 if (st%randomization == par_independent) then
1656 call zmf_random(mesh, zpsi(:, 1), &
1657 pre_shift = mesh%pv%xlocal-1, &
1658 post_shift = mesh%pv%np_global - mesh%pv%xlocal - mesh%np + 1, &
1659 normalized = normalized)
1660 ! Ensures that the grid points are properly distributed in the domain parallel case
1661 if(mesh%parallel_in_domains) then
1662 call batch_init(ffb, zpsi(:,1))
1663 call zmesh_batch_exchange_points(mesh, ffb, backward_map = .true.)
1664 call ffb%end()
1665 end if
1666 else
1667 call zmf_random(mesh, zpsi(:, 1), normalized = normalized)
1668 end if
1669 if (.not. state_kpt_is_local(st, ist, ik)) cycle
1670 call states_elec_set_state(st, mesh, ist, ik, zpsi)
1671 end if
1672 end do
1673 end do
1674
1675 case (spinors)
1676
1677 assert(states_are_complex(st))
1678
1679 if (st%fixed_spins) then
1680
1681 do ik = ikpt_start, ikpt_end
1682 ikpoint = st%d%get_kpoint_index(ik)
1683 do ist = ist_start, ist_end
1684 if (kpoints_point_is_gamma(kpoints, ikpoint)) then
1685 if (st%randomization == par_independent) then
1686 call dmf_random(mesh, dpsi(:, 1), &
1687 pre_shift = mesh%pv%xlocal-1, &
1688 post_shift = mesh%pv%np_global - mesh%pv%xlocal - mesh%np + 1, &
1689 normalized = normalized)
1690 ! Ensures that the grid points are properly distributed in the domain parallel case
1691 if(mesh%parallel_in_domains) then
1692 call batch_init(ffb, dpsi(:,1))
1693 call dmesh_batch_exchange_points(mesh, ffb, backward_map = .true.)
1694 call ffb%end()
1695 end if
1696 else
1697 call dmf_random(mesh, dpsi(:, 1), normalized = normalized)
1698 if (.not. state_kpt_is_local(st, ist, ik)) cycle
1699 end if
1700 do ip = 1, mesh%np
1701 zpsi(ip,1) = cmplx(dpsi(ip,1), m_zero, real64)
1702 end do
1703 call states_elec_set_state(st, mesh, ist, ik, zpsi)
1704 else
1705 if (st%randomization == par_independent) then
1706 call zmf_random(mesh, zpsi(:, 1), &
1707 pre_shift = mesh%pv%xlocal-1, &
1708 post_shift = mesh%pv%np_global - mesh%pv%xlocal - mesh%np + 1, &
1709 normalized = normalized)
1710 ! Ensures that the grid points are properly distributed in the domain parallel case
1711 if(mesh%parallel_in_domains) then
1712 call batch_init(ffb, zpsi(:,1))
1713 call zmesh_batch_exchange_points(mesh, ffb, backward_map = .true.)
1714 call ffb%end()
1715 end if
1716 else
1717 call zmf_random(mesh, zpsi(:, 1), normalized = normalized)
1718 if (.not. state_kpt_is_local(st, ist, ik)) cycle
1719 end if
1720 end if
1721 if (.not. state_kpt_is_local(st, ist, ik)) cycle
1722 ! In this case, the spinors are made of a spatial part times a vector [alpha beta]^T in
1723 ! spin space (i.e., same spatial part for each spin component). So (alpha, beta)
1724 ! determines the spin values. The values of (alpha, beta) can be be obtained
1725 ! with simple formulae from <Sx>, <Sy>, <Sz>.
1726 !
1727 ! Note that here we orthonormalize the orbital part. This ensures that the spinors
1728 ! are untouched later in the general orthonormalization, and therefore the spin values
1729 ! of each spinor remain the same.
1730 safe_allocate(zpsi2(1:mesh%np))
1731 do jst = ist_start, ist - 1
1732 call states_elec_get_state(st, mesh, 1, jst, ik, zpsi2)
1733 zdotp_tmp = zmf_dotp(mesh, zpsi(:, 1), zpsi2)
1734 call lalg_axpy(mesh%np, -zdotp_tmp, zpsi2, zpsi(:, 1))
1735 end do
1736 safe_deallocate_a(zpsi2)
1737
1738 call zmf_normalize(mesh, 1, zpsi)
1739 zpsi(1:mesh%np, 2) = zpsi(1:mesh%np, 1)
1740
1741 alpha = cmplx(sqrt(m_half + st%spin(3, ist, ik)), m_zero, real64)
1742 beta = cmplx(sqrt(m_one - abs(alpha)**2), m_zero, real64)
1743 if (abs(alpha) > m_epsilon) then
1744 beta = cmplx(st%spin(1, ist, ik) / abs(alpha), st%spin(2, ist, ik) / abs(alpha), real64)
1745 end if
1746 call lalg_scal(mesh%np, alpha, zpsi(:, 1))
1747 call lalg_scal(mesh%np, beta, zpsi(:, 2))
1748 call states_elec_set_state(st, mesh, ist, ik, zpsi)
1749 end do
1750 end do
1751 else
1752 do ik = ikpt_start, ikpt_end
1753 do ist = ist_start, ist_end
1754 do id = 1, st%d%dim
1755 if (st%randomization == par_independent) then
1756 call zmf_random(mesh, zpsi(:, id), &
1757 pre_shift = mesh%pv%xlocal-1, &
1758 post_shift = mesh%pv%np_global - mesh%pv%xlocal - mesh%np + 1, &
1759 normalized = .false.)
1760 ! Ensures that the grid points are properly distributed in the domain parallel case
1761 if(mesh%parallel_in_domains) then
1762 call batch_init(ffb, zpsi(:, id))
1763 call zmesh_batch_exchange_points(mesh, ffb, backward_map = .true.)
1764 call ffb%end()
1765 end if
1766 else
1767 call zmf_random(mesh, zpsi(:, id), normalized = .false.)
1768 end if
1769 end do
1770 ! We need to generate the wave functions even if not using them in order to be consistent
1771 ! with the random seed in parallel runs.
1772 if (.not. state_kpt_is_local(st, ist, ik)) cycle
1773 ! Note that mf_random normalizes each spin channel independently to 1.
1774 ! Therefore we need to renormalize the spinor:
1775 if (normalized_) call zmf_normalize(mesh, st%d%dim, zpsi)
1776 call states_elec_set_state(st, mesh, ist, ik, zpsi)
1777 end do
1778 end do
1779 end if
1780
1781 end select
1782
1783 safe_deallocate_a(dpsi)
1784 safe_deallocate_a(zpsi)
1785
1787 end subroutine states_elec_generate_random
1788
1789 ! ---------------------------------------------------------
1791 !
1792 subroutine states_elec_fermi(st, namespace, mesh, compute_spin)
1793 type(states_elec_t), intent(inout) :: st
1794 type(namespace_t), intent(in) :: namespace
1795 class(mesh_t), intent(in) :: mesh
1796 logical, optional, intent(in) :: compute_spin
1797
1799 integer :: ist, ik
1800 real(real64) :: charge
1801 complex(real64), allocatable :: zpsi(:, :)
1802
1803 push_sub(states_elec_fermi)
1804
1805 call smear_find_fermi_energy(st%smear, namespace, st%eigenval, st%occ, st%qtot, &
1806 st%nik, st%nst, st%kweights)
1807
1808 call smear_fill_occupations(st%smear, st%eigenval, st%occ, st%kweights, st%nik, st%nst)
1809
1810 ! check if everything is OK
1811 charge = m_zero
1812 do ist = 1, st%nst
1813 charge = charge + sum(st%occ(ist, 1:st%nik) * st%kweights(1:st%nik))
1814 end do
1815 if (abs(charge-st%qtot) > 1e-6_real64) then
1816 message(1) = 'Occupations do not integrate to total charge.'
1817 write(message(2), '(6x,f12.8,a,f12.8)') charge, ' != ', st%qtot
1818 call messages_warning(2, namespace=namespace)
1819 if (charge < m_epsilon) then
1820 message(1) = "There don't seem to be any electrons at all!"
1821 call messages_fatal(1, namespace=namespace)
1822 end if
1823 end if
1824
1825 if (st%d%ispin == spinors .and. optional_default(compute_spin,.true.)) then
1826 assert(states_are_complex(st))
1827
1828 st%spin(:,:,:) = m_zero
1829
1830 safe_allocate(zpsi(1:mesh%np, st%d%dim))
1831 do ik = st%d%kpt%start, st%d%kpt%end
1832 do ist = st%st_start, st%st_end
1833 call states_elec_get_state(st, mesh, ist, ik, zpsi)
1834 st%spin(1:3, ist, ik) = state_spin(mesh, zpsi)
1835 end do
1836 end do
1837 safe_deallocate_a(zpsi)
1838
1839 if (st%parallel_in_states .or. st%d%kpt%parallel) then
1840 call comm_allreduce(st%st_kpt_mpi_grp, st%spin)
1841 end if
1842
1843 end if
1844
1845 pop_sub(states_elec_fermi)
1846 end subroutine states_elec_fermi
1847
1848
1849 ! ---------------------------------------------------------
1851 !
1852 function states_elec_eigenvalues_sum(st, alt_eig) result(tot)
1853 type(states_elec_t), intent(in) :: st
1854 real(real64), optional, intent(in) :: alt_eig(st%st_start:, st%d%kpt%start:)
1856 real(real64) :: tot
1857
1858 integer :: ik
1859
1861
1862 tot = m_zero
1863 do ik = st%d%kpt%start, st%d%kpt%end
1864 if (present(alt_eig)) then
1865 tot = tot + st%kweights(ik) * sum(st%occ(st%st_start:st%st_end, ik) * &
1866 alt_eig(st%st_start:st%st_end, ik))
1867 else
1868 tot = tot + st%kweights(ik) * sum(st%occ(st%st_start:st%st_end, ik) * &
1869 st%eigenval(st%st_start:st%st_end, ik))
1870 end if
1871 end do
1872
1873 if (st%parallel_in_states .or. st%d%kpt%parallel) call comm_allreduce(st%st_kpt_mpi_grp, tot)
1874
1876 end function states_elec_eigenvalues_sum
1877
1878
1880 subroutine states_elec_distribute_nodes(st, namespace, mc)
1881 type(states_elec_t), intent(inout) :: st
1882 type(namespace_t), intent(in) :: namespace
1883 type(multicomm_t), intent(in) :: mc
1884
1885 logical :: default_scalapack_compatible
1886
1888
1889 ! TODO(Alex) Issue #820. This is superflous. These defaults are set in initialisation of
1890 ! states, and in the state distribution instance
1891 ! Defaults.
1892 st%node(:) = 0
1893 st%st_start = 1
1894 st%st_end = st%nst
1895 st%lnst = st%nst
1896 st%parallel_in_states = .false.
1897
1898 call mpi_grp_init(st%mpi_grp, mc%group_comm(p_strategy_states))
1899 call mpi_grp_init(st%system_grp, mc%master_comm)
1900 call mpi_grp_init(st%dom_st_kpt_mpi_grp, mc%dom_st_kpt_comm)
1901 call mpi_grp_init(st%dom_st_mpi_grp, mc%dom_st_comm)
1902 call mpi_grp_init(st%st_kpt_mpi_grp, mc%st_kpt_comm)
1903
1904 default_scalapack_compatible = calc_mode_par%scalapack_compat() .and. .not. st%d%kpt%parallel
1905
1906 !%Variable ScaLAPACKCompatible
1907 !%Type logical
1908 !%Section Execution::Parallelization
1909 !%Description
1910 !% Whether to use a layout for states parallelization which is compatible with ScaLAPACK.
1911 !% The default is yes for <tt>CalculationMode = gs, unocc, go</tt> without k-point parallelization,
1912 !% and no otherwise. (Setting to other than default is experimental.)
1913 !% The value must be yes if any ScaLAPACK routines are called in the course of the run;
1914 !% it must be set by hand for <tt>td</tt> with <tt>TDDynamics = bo</tt>.
1915 !% This variable has no effect unless you are using states parallelization and have linked ScaLAPACK.
1916 !% Note: currently, use of ScaLAPACK is not compatible with task parallelization (<i>i.e.</i> slaves).
1917 !%End
1918 call parse_variable(namespace, 'ScaLAPACKCompatible', default_scalapack_compatible, st%scalapack_compatible)
1919
1920#ifdef HAVE_SCALAPACK
1921 if (default_scalapack_compatible .neqv. st%scalapack_compatible) then
1922 call messages_experimental('Setting ScaLAPACKCompatible to other than default', namespace=namespace)
1923 end if
1924
1925 if (st%scalapack_compatible) then
1926 if (multicomm_have_slaves(mc)) then
1927 call messages_not_implemented("ScaLAPACK usage with task parallelization (slaves)", namespace=namespace)
1928 end if
1929 call blacs_proc_grid_init(st%dom_st_proc_grid, st%dom_st_mpi_grp)
1930 end if
1931#else
1932 st%scalapack_compatible = .false.
1933#endif
1934
1936
1937#ifdef HAVE_MPI
1938 call multicomm_create_all_pairs(st%mpi_grp, st%ap)
1939#endif
1940
1941 if (st%nst < st%mpi_grp%size) then
1942 message(1) = "Have more processors than necessary"
1943 write(message(2),'(i4,a,i4,a)') st%mpi_grp%size, " processors and ", st%nst, " states."
1944 call messages_fatal(2, namespace=namespace)
1945 end if
1946
1947 call distributed_init(st%dist, st%nst, st%mpi_grp%comm, "states", scalapack_compat = st%scalapack_compatible)
1948
1949 st%parallel_in_states = st%dist%parallel
1950
1951 ! TODO(Alex) Issue #820. Remove lnst, st_start, st_end and node, as they are all contained within dist
1952 st%st_start = st%dist%start
1953 st%st_end = st%dist%end
1954 st%lnst = st%dist%nlocal()
1955 st%node(1:st%nst) = st%dist%process(1:st%nst)
1956
1957 end if
1958
1960
1962 end subroutine states_elec_distribute_nodes
1963
1964
1970 !
1971 subroutine states_elec_calc_quantities(gr, st, kpoints, nlcc, &
1972 kinetic_energy_density, paramagnetic_current, density_gradient, density_laplacian, &
1973 gi_kinetic_energy_density, st_end)
1974 type(grid_t), intent(in) :: gr
1975 type(states_elec_t), intent(in) :: st
1976 type(kpoints_t), intent(in) :: kpoints
1977 logical, intent(in) :: nlcc
1978 real(real64), contiguous, optional, target, intent(out) :: kinetic_energy_density(:,:)
1979 real(real64), contiguous, optional, target, intent(out) :: paramagnetic_current(:,:,:)
1980 real(real64), contiguous, optional, intent(out) :: density_gradient(:,:,:)
1981 real(real64), contiguous, optional, intent(out) :: density_laplacian(:,:)
1982 real(real64), contiguous, optional, intent(out) :: gi_kinetic_energy_density(:,:)
1983 integer, optional, intent(in) :: st_end
1984
1985 real(real64), pointer, contiguous :: jp(:, :, :)
1986 real(real64), pointer, contiguous :: tau(:, :)
1987 complex(real64), allocatable :: wf_psi(:,:), gwf_psi(:,:,:), wf_psi_conj(:,:), lwf_psi(:,:)
1988 real(real64), allocatable :: abs_wf_psi(:,:), abs_gwf_psi(:,:)
1989 complex(real64), allocatable :: psi_gpsi(:,:)
1990 complex(real64) :: c_tmp
1991 integer :: is, ik, ist, i_dim, st_dim, ii, st_end_
1992 real(real64) :: ww, kpoint(gr%der%dim)
1993 logical :: something_to_do
1994
1995 call profiling_in("STATES_CALC_QUANTITIES")
1996
1998
1999 st_end_ = min(st%st_end, optional_default(st_end, st%st_end))
2000
2001 something_to_do = present(kinetic_energy_density) .or. present(gi_kinetic_energy_density) .or. &
2002 present(paramagnetic_current) .or. present(density_gradient) .or. present(density_laplacian)
2003 assert(something_to_do)
2004
2005 safe_allocate( wf_psi(1:gr%np_part, 1:st%d%dim))
2006 safe_allocate( wf_psi_conj(1:gr%np_part, 1:st%d%dim))
2007 safe_allocate(gwf_psi(1:gr%np, 1:gr%der%dim, 1:st%d%dim))
2008 safe_allocate(abs_wf_psi(1:gr%np, 1:st%d%dim))
2009 safe_allocate(abs_gwf_psi(1:gr%np, 1:st%d%dim))
2010 safe_allocate(psi_gpsi(1:gr%np, 1:st%d%dim))
2011 if (present(density_laplacian)) then
2012 safe_allocate(lwf_psi(1:gr%np, 1:st%d%dim))
2013 end if
2014
2015 nullify(tau)
2016 if (present(kinetic_energy_density)) tau => kinetic_energy_density
2017
2018 nullify(jp)
2019 if (present(paramagnetic_current)) jp => paramagnetic_current
2020
2021 ! for the gauge-invariant kinetic energy density we need the
2022 ! current and the kinetic energy density
2023 if (present(gi_kinetic_energy_density)) then
2024 if (.not. present(paramagnetic_current) .and. states_are_complex(st)) then
2025 safe_allocate(jp(1:gr%np, 1:gr%der%dim, 1:st%d%nspin))
2026 end if
2027 if (.not. present(kinetic_energy_density)) then
2028 safe_allocate(tau(1:gr%np, 1:st%d%nspin))
2029 end if
2030 end if
2031
2032 if (associated(tau)) tau = m_zero
2033 if (associated(jp)) jp = m_zero
2034 if (present(density_gradient)) density_gradient(:,:,:) = m_zero
2035 if (present(density_laplacian)) density_laplacian(:,:) = m_zero
2036 if (present(gi_kinetic_energy_density)) gi_kinetic_energy_density = m_zero
2037
2038 do ik = st%d%kpt%start, st%d%kpt%end
2039
2040 kpoint(1:gr%der%dim) = kpoints%get_point(st%d%get_kpoint_index(ik))
2041 is = st%d%get_spin_index(ik)
2042
2043 do ist = st%st_start, st_end_
2044 ww = st%kweights(ik)*st%occ(ist, ik)
2045 if (abs(ww) <= m_epsilon) cycle
2046
2047 ! all calculations will be done with complex wavefunctions
2048 call states_elec_get_state(st, gr, ist, ik, wf_psi)
2049
2050 do st_dim = 1, st%d%dim
2051 call boundaries_set(gr%der%boundaries, gr, wf_psi(:, st_dim))
2052 end do
2053
2054 ! calculate gradient of the wavefunction
2055 do st_dim = 1, st%d%dim
2056 call zderivatives_grad(gr%der, wf_psi(:,st_dim), gwf_psi(:,:,st_dim), set_bc = .false.)
2057 end do
2058
2059 ! calculate the Laplacian of the wavefunction
2060 if (present(density_laplacian)) then
2061 do st_dim = 1, st%d%dim
2062 call zderivatives_lapl(gr%der, wf_psi(:,st_dim), lwf_psi(:,st_dim), ghost_update = .false., set_bc = .false.)
2063 end do
2064 end if
2065
2066 ! We precompute some quantites, to avoid to compute it many times
2067 wf_psi_conj(1:gr%np, 1:st%d%dim) = conjg(wf_psi(1:gr%np,1:st%d%dim))
2068 !$omp parallel do collapse(2)
2069 do st_dim = 1, st%d%dim
2070 do ii = 1, gr%np
2071 abs_wf_psi(ii, st_dim) = real(wf_psi_conj(ii, st_dim) * wf_psi(ii, st_dim), real64)
2072 end do
2073 end do
2074 !$omp end parallel do
2075
2076 if (present(density_laplacian)) then
2077 !$omp parallel do
2078 do ii = 1, gr%np
2079 density_laplacian(ii, is) = density_laplacian(ii, is) + &
2080 ww * m_two*real(wf_psi_conj(ii, 1) * lwf_psi(ii, 1), real64)
2081 end do
2082 !$omp end parallel do
2083 if (st%d%ispin == spinors) then
2084 !$omp parallel do
2085 do ii = 1, gr%np
2086 density_laplacian(ii, 2) = density_laplacian(ii, 2) + &
2087 ww * m_two*real(wf_psi_conj(ii, 2) * lwf_psi(ii, 2), real64)
2088 end do
2089 !$omp end parallel do
2090 !$omp parallel do private(c_tmp)
2091 do ii = 1, gr%np
2092 c_tmp = ww*(lwf_psi(ii, 1) * wf_psi_conj(ii, 2) + wf_psi(ii, 1) * conjg(lwf_psi(ii, 2)))
2093 density_laplacian(ii, 3) = density_laplacian(ii, 3) + real(c_tmp, real64)
2094 density_laplacian(ii, 4) = density_laplacian(ii, 4) + aimag(c_tmp)
2095 end do
2096 end if
2097 end if
2098
2099 if (associated(tau)) then
2100 call lalg_axpy(gr%np, ww * sum(kpoint(1:gr%der%dim)**2), abs_wf_psi(:, 1), tau(:, is))
2101 if (st%d%ispin == spinors) then
2102 call lalg_axpy(gr%np, ww * sum(kpoint(1:gr%der%dim)**2), abs_wf_psi(:, 2), tau(:, 2))
2103
2104 !$omp parallel do private(c_tmp)
2105 do ii = 1, gr%np
2106 c_tmp = ww * sum(kpoint(1:gr%der%dim)**2) * wf_psi(ii, 1) * wf_psi_conj(ii, 2)
2107 tau(ii, 3) = tau(ii, 3) + real(c_tmp, real64)
2108 tau(ii, 4) = tau(ii, 4) + aimag(c_tmp)
2109 end do
2110 end if
2111 end if
2112
2113 do i_dim = 1, gr%der%dim
2114
2115 ! We precompute some quantites, to avoid to compute them many times
2116 !$omp parallel private(st_dim, ii)
2117 do st_dim = 1, st%d%dim
2118 !$omp do
2119 do ii = 1, gr%np
2120 psi_gpsi(ii, st_dim) = wf_psi_conj(ii, st_dim) * gwf_psi(ii, i_dim, st_dim)
2121 end do
2122 end do
2123 do st_dim = 1, st%d%dim
2124 !$omp do
2125 do ii = 1, gr%np
2126 abs_gwf_psi(ii, st_dim) = real(conjg(gwf_psi(ii, i_dim, st_dim)) &
2127 * gwf_psi(ii, i_dim, st_dim), real64)
2128 end do
2129 end do
2130 !$omp end parallel
2131
2132 if (present(density_gradient)) then
2133 !$omp parallel do
2134 do ii = 1, gr%np
2135 density_gradient(ii, i_dim, is) = density_gradient(ii, i_dim, is) &
2136 + ww * m_two * real(psi_gpsi(ii, 1), real64)
2137 end do
2138 !$omp end parallel do
2139 if (st%d%ispin == spinors) then
2140 !$omp parallel do
2141 do ii = 1, gr%np
2142 density_gradient(ii, i_dim, 2) = density_gradient(ii, i_dim, 2) &
2143 + ww * m_two*real(psi_gpsi(ii, 2), real64)
2144 end do
2145 !$omp end parallel do
2146 !$omp parallel do private(c_tmp)
2147 do ii = 1, gr%np
2148 c_tmp = ww * (gwf_psi(ii, i_dim, 1) * wf_psi_conj(ii, 2) + wf_psi(ii, 1) * conjg(gwf_psi(ii, i_dim, 2)))
2149 density_gradient(ii, i_dim, 3) = density_gradient(ii, i_dim, 3) + real(c_tmp, real64)
2150 density_gradient(ii, i_dim, 4) = density_gradient(ii, i_dim, 4) + aimag(c_tmp)
2151 end do
2152 end if
2153 end if
2154
2155 if (present(density_laplacian)) then
2156 call lalg_axpy(gr%np, ww*m_two, abs_gwf_psi(:,1), density_laplacian(:,is))
2157 if (st%d%ispin == spinors) then
2158 call lalg_axpy(gr%np, ww*m_two, abs_gwf_psi(:,2), density_laplacian(:,2))
2159 !$omp parallel do private(c_tmp)
2160 do ii = 1, gr%np
2161 c_tmp = m_two * ww * gwf_psi(ii, i_dim, 1) * conjg(gwf_psi(ii, i_dim, 2))
2162 density_laplacian(ii, 3) = density_laplacian(ii, 3) + real(c_tmp, real64)
2163 density_laplacian(ii, 4) = density_laplacian(ii, 4) + aimag(c_tmp)
2164 end do
2165 end if
2166 end if
2167
2168 ! the expression for the paramagnetic current with spinors is
2169 ! j = ( jp(1) jp(3) + i jp(4) )
2170 ! (-jp(3) + i jp(4) jp(2) )
2171 if (associated(jp)) then
2172 if (.not.(states_are_real(st))) then
2173 !$omp parallel do
2174 do ii = 1, gr%np
2175 jp(ii, i_dim, is) = jp(ii, i_dim, is) + &
2176 ww*(aimag(psi_gpsi(ii, 1)) - abs_wf_psi(ii, 1)*kpoint(i_dim))
2177 end do
2178 !$omp end parallel do
2179 if (st%d%ispin == spinors) then
2180 !$omp parallel do
2181 do ii = 1, gr%np
2182 jp(ii, i_dim, 2) = jp(ii, i_dim, 2) + &
2183 ww*( aimag(psi_gpsi(ii, 2)) - abs_wf_psi(ii, 2)*kpoint(i_dim))
2184 end do
2185 !$omp end parallel do
2186 !$omp parallel do private(c_tmp)
2187 do ii = 1, gr%np
2188 c_tmp = -ww*m_half*m_zi*(gwf_psi(ii, i_dim, 1)*wf_psi_conj(ii, 2) - wf_psi(ii, 1)*conjg(gwf_psi(ii, i_dim, 2)) &
2189 - m_two * m_zi*wf_psi(ii, 1)*wf_psi_conj(ii, 2)*kpoint(i_dim))
2190 jp(ii, i_dim, 3) = jp(ii, i_dim, 3) + real(c_tmp, real64)
2191 jp(ii, i_dim, 4) = jp(ii, i_dim, 4) + aimag(c_tmp)
2192 end do
2193 end if
2194 end if
2195 end if
2196
2197 ! the expression for the paramagnetic current with spinors is
2198 ! t = ( tau(1) tau(3) + i tau(4) )
2199 ! ( tau(3) - i tau(4) tau(2) )
2200 if (associated(tau)) then
2201 !$omp parallel do
2202 do ii = 1, gr%np
2203 tau(ii, is) = tau(ii, is) + ww*(abs_gwf_psi(ii,1) &
2204 - m_two*aimag(psi_gpsi(ii, 1))*kpoint(i_dim))
2205 end do
2206 !$omp end parallel do
2207 if (st%d%ispin == spinors) then
2208 !$omp parallel do
2209 do ii = 1, gr%np
2210 tau(ii, 2) = tau(ii, 2) + ww*(abs_gwf_psi(ii, 2) &
2211 - m_two*aimag(psi_gpsi(ii, 2))*kpoint(i_dim))
2212 end do
2213 !$omp end parallel do
2214 !$omp parallel do private(c_tmp)
2215 do ii = 1, gr%np
2216 c_tmp = ww * ( gwf_psi(ii, i_dim, 1)*conjg(gwf_psi(ii, i_dim, 2)) &
2217 + m_zi * (gwf_psi(ii,i_dim,1)*wf_psi_conj(ii, 2) &
2218 - wf_psi(ii, 1)*conjg(gwf_psi(ii,i_dim,2)))*kpoint(i_dim))
2219 tau(ii, 3) = tau(ii, 3) + real(c_tmp, real64)
2220 tau(ii, 4) = tau(ii, 4) + aimag(c_tmp)
2221 end do
2222 end if
2223 end if
2224
2225 end do
2226
2227 end do
2228 end do
2229
2230 safe_deallocate_a(wf_psi_conj)
2231 safe_deallocate_a(abs_wf_psi)
2232 safe_deallocate_a(abs_gwf_psi)
2233 safe_deallocate_a(psi_gpsi)
2234
2235 if (.not. present(gi_kinetic_energy_density)) then
2236 if (.not. present(paramagnetic_current)) then
2237 safe_deallocate_p(jp)
2238 end if
2239 if (.not. present(kinetic_energy_density)) then
2240 safe_deallocate_p(tau)
2241 end if
2242 end if
2243
2244 if (st%parallel_in_states .or. st%d%kpt%parallel) call reduce_all(st%st_kpt_mpi_grp)
2245
2246 ! We have to symmetrize everything as they are calculated from the
2247 ! wavefunctions.
2248 ! This must be done before compute the gauge-invariant kinetic energy density
2249 if (st%symmetrize_density) then
2250 do is = 1, st%d%nspin
2251 if (associated(tau)) then
2252 call dgrid_symmetrize_scalar_field(gr, tau(:, is), suppress_warning = .true.)
2253 end if
2254
2255 if (present(density_laplacian)) then
2256 call dgrid_symmetrize_scalar_field(gr, density_laplacian(:, is), suppress_warning = .true.)
2257 end if
2258
2259 if (associated(jp)) then
2260 call dgrid_symmetrize_vector_field(gr, jp(:, :, is), suppress_warning = .true.)
2261 end if
2262
2263 if (present(density_gradient)) then
2264 call dgrid_symmetrize_vector_field(gr, density_gradient(:, :, is), suppress_warning = .true.)
2265 end if
2266 end do
2267 end if
2268
2269
2270 if (allocated(st%rho_core) .and. nlcc .and. (present(density_laplacian) .or. present(density_gradient))) then
2271 do ii = 1, gr%np
2272 wf_psi(ii, 1) = st%rho_core(ii)/st%d%spin_channels
2273 end do
2274
2275 call boundaries_set(gr%der%boundaries, gr, wf_psi(:, 1))
2276
2277 if (present(density_gradient)) then
2278 ! calculate gradient of the NLCC
2279 call zderivatives_grad(gr%der, wf_psi(:,1), gwf_psi(:,:,1), set_bc = .false.)
2280 do is = 1, st%d%spin_channels
2281 !$omp parallel do collapse(2)
2282 do i_dim = 1, gr%der%dim
2283 do ii = 1, gr%np
2284 density_gradient(ii, i_dim, is) = density_gradient(ii, i_dim, is) + &
2285 real(gwf_psi(ii, i_dim, 1), real64)
2286 end do
2287 end do
2288 !$omp end parallel do
2289 end do
2290 end if
2291
2292 ! calculate the Laplacian of the wavefunction
2293 if (present(density_laplacian)) then
2294 call zderivatives_lapl(gr%der, wf_psi(:,1), lwf_psi(:,1), set_bc = .false.)
2295
2296 !$omp parallel private(is, ii)
2297 do is = 1, st%d%spin_channels
2298 !$omp do
2299 do ii = 1, gr%np
2300 density_laplacian(ii, is) = density_laplacian(ii, is) + real(lwf_psi(ii, 1))
2301 end do
2302 end do
2303 !$omp end parallel
2304 end if
2305 end if
2306
2307 !If we freeze some of the orbitals, we need to had the contributions here
2308 !Only in the case we are not computing it
2309 if (allocated(st%frozen_tau) .and. .not. present(st_end) .and. associated(tau)) then
2310 call lalg_axpy(gr%np, st%d%nspin, m_one, st%frozen_tau, tau)
2311 end if
2312 if (allocated(st%frozen_gdens) .and. .not. present(st_end) .and. present(density_gradient)) then
2313 do is = 1, st%d%nspin
2314 call lalg_axpy(gr%np, gr%der%dim, m_one, st%frozen_gdens(:,:,is), density_gradient(:,:,is))
2315 end do
2316 end if
2317 if (allocated(st%frozen_ldens) .and. .not. present(st_end) .and. present(density_laplacian)) then
2318 call lalg_axpy(gr%np, st%d%nspin, m_one, st%frozen_ldens, density_laplacian)
2319 end if
2320
2321 safe_deallocate_a(wf_psi)
2322 safe_deallocate_a(gwf_psi)
2323 safe_deallocate_a(lwf_psi)
2324
2325
2326 !We compute the gauge-invariant kinetic energy density
2327 if (present(gi_kinetic_energy_density)) then
2328 do is = 1, st%d%nspin
2329 assert(associated(tau))
2330 gi_kinetic_energy_density(1:gr%np, is) = tau(1:gr%np, is)
2331 end do
2332 if (states_are_complex(st)) then
2333 assert(associated(jp))
2334 if (st%d%ispin /= spinors) then
2335 do is = 1, st%d%nspin
2336 !$omp parallel do
2337 do ii = 1, gr%np
2338 if (st%rho(ii, is) < 1.0e-7_real64) cycle
2339 gi_kinetic_energy_density(ii, is) = &
2340 gi_kinetic_energy_density(ii, is) - sum(jp(ii,1:gr%der%dim, is)**2)/st%rho(ii, is)
2341 end do
2342 end do
2343 else ! Note that this is only the U(1) part of the gauge-invariant KED
2344 !$omp parallel do
2345 do ii = 1, gr%np
2346 gi_kinetic_energy_density(ii, 1) = gi_kinetic_energy_density(ii, 1) &
2347 - sum(jp(ii,1:gr%der%dim, 1)**2)/(safe_tol(st%rho(ii, 1),m_epsilon))
2348 gi_kinetic_energy_density(ii, 2) = gi_kinetic_energy_density(ii, 2) &
2349 - sum(jp(ii,1:gr%der%dim, 2)**2)/(safe_tol(st%rho(ii, 2),m_epsilon))
2350 gi_kinetic_energy_density(ii, 3) = &
2351 gi_kinetic_energy_density(ii, 3) - sum(jp(ii,1:gr%der%dim, 3)**2 + jp(ii,1:gr%der%dim, 4)**2) &
2352 /(safe_tol((st%rho(ii, 3)**2 + st%rho(ii, 4)**2), m_epsilon))*st%rho(ii, 3)
2353 gi_kinetic_energy_density(ii, 4) = &
2354 gi_kinetic_energy_density(ii, 4) + sum(jp(ii,1:gr%der%dim, 3)**2 + jp(ii,1:gr%der%dim, 4)**2) &
2355 /(safe_tol((st%rho(ii, 3)**2 + st%rho(ii, 4)**2), m_epsilon))*st%rho(ii, 4)
2356 end do
2357 end if
2358 end if
2359 end if
2360
2361 if (.not. present(kinetic_energy_density)) then
2362 safe_deallocate_p(tau)
2363 end if
2364 if (.not. present(paramagnetic_current)) then
2365 safe_deallocate_p(jp)
2366 end if
2367
2368
2370
2371 call profiling_out("STATES_CALC_QUANTITIES")
2372
2373 contains
2374
2375 subroutine reduce_all(grp)
2376 type(mpi_grp_t), intent(in) :: grp
2377
2379
2380 if (associated(tau)) call comm_allreduce(grp, tau, dim = (/gr%np, st%d%nspin/))
2381
2382 if (present(density_laplacian)) call comm_allreduce(grp, density_laplacian, dim = (/gr%np, st%d%nspin/))
2383
2384 do is = 1, st%d%nspin
2385 if (associated(jp)) call comm_allreduce(grp, jp(:, :, is), dim = (/gr%np, gr%der%dim/))
2386
2387 if (present(density_gradient)) then
2388 call comm_allreduce(grp, density_gradient(:, :, is), dim = (/gr%np, gr%der%dim/))
2389 end if
2390 end do
2391
2393 end subroutine reduce_all
2394
2395 end subroutine states_elec_calc_quantities
2396
2397
2398 ! ---------------------------------------------------------
2400 !
2401 function state_spin(mesh, f1) result(spin)
2402 type(mesh_t), intent(in) :: mesh
2403 complex(real64), contiguous, intent(in) :: f1(:, :)
2404 real(real64) :: spin(1:3)
2405
2406 complex(real64) :: z
2407
2408 push_sub(state_spin)
2409
2410 z = zmf_dotp(mesh, f1(:, 1) , f1(:, 2))
2411
2412 spin(1) = m_two*real(z, real64)
2413 spin(2) = m_two*aimag(z)
2414 spin(3) = zmf_nrm2(mesh, f1(:, 1))**2 - zmf_nrm2(mesh, f1(:, 2))**2
2415 spin = m_half*spin ! spin is half the sigma matrix.
2416
2417 pop_sub(state_spin)
2418 end function state_spin
2419
2420 ! ---------------------------------------------------------
2422 !
2423 logical function state_is_local(st, ist)
2424 type(states_elec_t), intent(in) :: st
2425 integer, intent(in) :: ist
2426
2427 push_sub(state_is_local)
2428
2429 state_is_local = ist >= st%st_start.and.ist <= st%st_end
2430
2431 pop_sub(state_is_local)
2432 end function state_is_local
2433
2434 ! ---------------------------------------------------------
2436 !
2437 logical function state_kpt_is_local(st, ist, ik)
2438 type(states_elec_t), intent(in) :: st
2439 integer, intent(in) :: ist
2440 integer, intent(in) :: ik
2441
2442 push_sub(state_kpt_is_local)
2443
2444 state_kpt_is_local = ist >= st%st_start .and. ist <= st%st_end .and. &
2445 ik >= st%d%kpt%start .and. ik <= st%d%kpt%end
2446
2447 pop_sub(state_kpt_is_local)
2448 end function state_kpt_is_local
2449
2450
2451 ! ---------------------------------------------------------
2453 real(real64) function states_elec_wfns_memory(st, mesh) result(memory)
2454 type(states_elec_t), intent(in) :: st
2455 class(mesh_t), intent(in) :: mesh
2457 push_sub(states_elec_wfns_memory)
2458 memory = m_zero
2459
2460 ! orbitals
2461 memory = memory + real(storage_size(0.0_real64)/8, real64) * &
2462 real(mesh%np_part_global, real64) *st%d%dim * real(st%nst, real64) *st%d%kpt%nglobal()
2463
2465 end function states_elec_wfns_memory
2466
2467 ! ---------------------------------------------------------
2469 !
2470 subroutine states_elec_pack(st, copy)
2471 class(states_elec_t), intent(inout) :: st
2472 logical, optional, intent(in) :: copy
2473
2474 integer :: iqn, ib
2475 integer(int64) :: max_mem, mem
2476
2477 push_sub(states_elec_pack)
2478
2479 ! nothing to do, already packed
2480 if (st%packed) then
2481 pop_sub(states_elec_pack)
2482 return
2483 end if
2484
2485 st%packed = .true.
2486
2487 if (accel_is_enabled()) then
2488 max_mem = accel_global_memory_size()
2489
2490 if (st%gpu_states_mem > m_one) then
2491 max_mem = int(st%gpu_states_mem, int64)*(1024_8)**2
2492 else if (st%gpu_states_mem < 0.0_real64) then
2493 max_mem = max_mem + int(st%gpu_states_mem, int64)*(1024_8)**2
2494 else
2495 max_mem = int(st%gpu_states_mem*real(max_mem, real64) , int64)
2496 end if
2497 else
2498 max_mem = huge(max_mem)
2499 end if
2500
2501 mem = 0
2502 qnloop: do iqn = st%d%kpt%start, st%d%kpt%end
2503 do ib = st%group%block_start, st%group%block_end
2505 mem = mem + st%group%psib(ib, iqn)%pack_total_size()
2506
2507 if (mem > max_mem) then
2508 call messages_write('Not enough device memory to store all states simultaneously.', new_line = .true.)
2509 call messages_write('Only ')
2510 call messages_write(ib - st%group%block_start)
2511 call messages_write(' of ')
2512 call messages_write(st%group%block_end - st%group%block_start + 1)
2513 call messages_write(' blocks will be stored in device memory.', new_line = .true.)
2514 call messages_warning()
2515 exit qnloop
2516 end if
2517
2518 call st%group%psib(ib, iqn)%do_pack(copy)
2519 end do
2520 end do qnloop
2521
2522 pop_sub(states_elec_pack)
2523 end subroutine states_elec_pack
2524
2525 ! ------------------------------------------------------------
2527 !
2528 subroutine states_elec_unpack(st, copy)
2529 class(states_elec_t), intent(inout) :: st
2530 logical, optional, intent(in) :: copy
2531
2532 integer :: iqn, ib
2533
2535
2536 if (st%packed) then
2537 st%packed = .false.
2538
2539 do iqn = st%d%kpt%start, st%d%kpt%end
2540 do ib = st%group%block_start, st%group%block_end
2541 if (st%group%psib(ib, iqn)%is_packed()) call st%group%psib(ib, iqn)%do_unpack(copy)
2542 end do
2543 end do
2544 end if
2545
2546 pop_sub(states_elec_unpack)
2547 end subroutine states_elec_unpack
2548
2549 ! -----------------------------------------------------------
2552 subroutine states_elec_write_info(st, namespace)
2553 class(states_elec_t), intent(in) :: st
2554 type(namespace_t), intent(in) :: namespace
2555
2556 push_sub(states_elec_write_info)
2557
2558 call messages_print_with_emphasis(msg="States", namespace=namespace)
2559
2560 write(message(1), '(a,f12.3)') 'Total electronic charge = ', st%qtot
2561 write(message(2), '(a,i8)') 'Number of states = ', st%nst
2562 write(message(3), '(a,i8)') 'States block-size = ', st%block_size
2563 call messages_info(3, namespace=namespace)
2564
2565 call messages_print_with_emphasis(namespace=namespace)
2566
2567 pop_sub(states_elec_write_info)
2568 end subroutine states_elec_write_info
2569
2571 subroutine states_elec_set_zero(st)
2572 class(states_elec_t), intent(inout) :: st
2573
2574 integer :: iqn, ib
2575
2576 push_sub(states_elec_set_zero)
2577
2578 do iqn = st%d%kpt%start, st%d%kpt%end
2579 do ib = st%group%block_start, st%group%block_end
2580 call batch_set_zero(st%group%psib(ib, iqn))
2581 end do
2582 end do
2583
2584 pop_sub(states_elec_set_zero)
2585 end subroutine states_elec_set_zero
2586
2587 ! ------------------------------------------------------------
2589 !
2590 integer pure function states_elec_block_min(st, ib) result(range)
2591 type(states_elec_t), intent(in) :: st
2592 integer, intent(in) :: ib
2593
2594 range = st%group%block_range(ib, 1)
2595 end function states_elec_block_min
2596
2597 ! ------------------------------------------------------------
2599 !
2600 integer pure function states_elec_block_max(st, ib) result(range)
2601 type(states_elec_t), intent(in) :: st
2602 integer, intent(in) :: ib
2603
2604 range = st%group%block_range(ib, 2)
2605 end function states_elec_block_max
2606
2607 ! ------------------------------------------------------------
2610 integer pure function states_elec_block_size(st, ib) result(size)
2611 type(states_elec_t), intent(in) :: st
2612 integer, intent(in) :: ib
2613
2614 size = st%group%block_size(ib)
2615 end function states_elec_block_size
2616
2617 ! ------------------------------------------------------------
2619 !
2620 logical pure function states_elec_all_states_gpu_packed(st) result(all_packed)
2621 type(states_elec_t), intent(in) :: st
2622
2623 integer :: ib, ik
2624
2625 all_packed = .true.
2626 do ik = st%d%kpt%start, st%d%kpt%end
2627 do ib = st%group%block_start, st%group%block_end
2628 if (st%group%psib(ib, ik)%status() /= batch_device_packed) then
2629 all_packed = .false.
2630 return
2631 end if
2632 end do
2633 end do
2635
2651 !
2652 subroutine occupied_states(st, namespace, ik, n_filled, n_partially_filled, n_half_filled, &
2653 filled, partially_filled, half_filled)
2654 type(states_elec_t), intent(in) :: st
2655 type(namespace_t), intent(in) :: namespace
2656 integer, intent(in) :: ik
2657 integer, intent(out) :: n_filled, n_partially_filled, n_half_filled
2658 integer, optional, intent(out) :: filled(:), partially_filled(:), half_filled(:)
2659
2660 integer :: ist
2661
2662 push_sub(occupied_states)
2663
2664 if (present(filled)) filled(:) = 0
2665 if (present(partially_filled)) partially_filled(:) = 0
2666 if (present(half_filled)) half_filled(:) = 0
2667 n_filled = 0
2668 n_partially_filled = 0
2669 n_half_filled = 0
2670
2671 select case (st%d%ispin)
2672 case (unpolarized)
2673 do ist = 1, st%nst
2674 if (abs(st%occ(ist, ik) - m_two) < m_min_occ) then
2675 n_filled = n_filled + 1
2676 if (present(filled)) filled(n_filled) = ist
2677 else if (abs(st%occ(ist, ik) - m_one) < m_min_occ) then
2678 n_half_filled = n_half_filled + 1
2679 if (present(half_filled)) half_filled(n_half_filled) = ist
2680 else if (st%occ(ist, ik) > m_min_occ) then
2681 n_partially_filled = n_partially_filled + 1
2682 if (present(partially_filled)) partially_filled(n_partially_filled) = ist
2683 else if (abs(st%occ(ist, ik)) > m_min_occ) then
2684 write(message(1),*) 'Internal error in occupied_states: Illegal occupation value ', st%occ(ist, ik)
2685 call messages_fatal(1, namespace=namespace)
2686 end if
2687 end do
2688 case (spin_polarized, spinors)
2689 do ist = 1, st%nst
2690 if (abs(st%occ(ist, ik)-m_one) < m_min_occ) then
2691 n_filled = n_filled + 1
2692 if (present(filled)) filled(n_filled) = ist
2693 else if (st%occ(ist, ik) > m_min_occ) then
2694 n_partially_filled = n_partially_filled + 1
2695 if (present(partially_filled)) partially_filled(n_partially_filled) = ist
2696 else if (abs(st%occ(ist, ik)) > m_min_occ) then
2697 write(message(1),*) 'Internal error in occupied_states: Illegal occupation value ', st%occ(ist, ik)
2698 call messages_fatal(1, namespace=namespace)
2699 end if
2700 end do
2701 end select
2702
2703 pop_sub(occupied_states)
2704 end subroutine occupied_states
2705
2706
2708 subroutine kpoints_distribute(this, mc)
2709 type(states_elec_t), intent(inout) :: this
2710 type(multicomm_t), intent(in) :: mc
2711
2712 push_sub(kpoints_distribute)
2713 call distributed_init(this%d%kpt, this%nik, mc%group_comm(p_strategy_kpoints), "k-points")
2714
2715 pop_sub(kpoints_distribute)
2716 end subroutine kpoints_distribute
2717
2718
2719 ! TODO(Alex) Issue #824. Consider converting this to a function to returns `st_kpt_task`
2720 ! as this is only called in a couple of places, or package with the `st_kpt_mpi_grp` when split
2721 ! from st instance
2724 type(states_elec_t), intent(inout) :: st
2725
2727
2728 if (.not. allocated(st%st_kpt_task)) then
2729 safe_allocate(st%st_kpt_task(0:st%st_kpt_mpi_grp%size-1, 1:4))
2730 end if
2731
2732 st%st_kpt_task(0:st%st_kpt_mpi_grp%size-1, :) = 0
2733 st%st_kpt_task(st%st_kpt_mpi_grp%rank, :) = [st%st_start, st%st_end, st%d%kpt%start, st%d%kpt%end]
2734
2735 if (st%parallel_in_states .or. st%d%kpt%parallel) then
2736 call comm_allreduce(st%st_kpt_mpi_grp, st%st_kpt_task)
2737 end if
2738
2741
2742 ! ---------------------------------------------------------
2744 !
2745 subroutine states_elec_choose_kpoints(st, kpoints, namespace)
2746 type(states_elec_t), target, intent(inout) :: st
2747 type(kpoints_t), intent(in) :: kpoints
2748 type(namespace_t), intent(in) :: namespace
2749
2750 integer :: ik, iq
2751 type(states_elec_dim_t), pointer :: dd
2752
2753
2755
2756 dd => st%d
2757
2758 st%nik = kpoints_number(kpoints)
2759
2760 if (dd%ispin == spin_polarized) st%nik = 2*st%nik
2761
2762 safe_allocate(st%kweights(1:st%nik))
2763
2764 do iq = 1, st%nik
2765 ik = dd%get_kpoint_index(iq)
2766 st%kweights(iq) = kpoints%get_weight(ik)
2767 end do
2768
2769 if (debug%info) call print_kpoints_debug
2771
2772 contains
2773 subroutine print_kpoints_debug
2774 integer :: iunit
2775
2777
2778 call io_mkdir('debug/', namespace)
2779 iunit = io_open('debug/kpoints', namespace, action = 'write')
2780 call kpoints%write_info(iunit=iunit, absolute_coordinates = .true.)
2781 call io_close(iunit)
2782
2784 end subroutine print_kpoints_debug
2785
2786 end subroutine states_elec_choose_kpoints
2787
2792 !
2793 function states_elec_calculate_dipole(this, gr) result(dipole)
2794 class(states_elec_t), intent(in) :: this
2795 class(mesh_t), intent(in) :: gr
2796 real(real64) :: dipole(1:gr%box%dim)
2797
2798 integer :: ispin
2799 real(real64) :: e_dip(1:gr%box%dim, this%d%spin_channels)
2800
2802
2803 do ispin = 1, this%d%spin_channels
2804 call dmf_dipole(gr, this%rho(:, ispin), e_dip(:, ispin))
2805 end do
2806
2807 dipole(:) = sum(e_dip(:, 1:this%d%spin_channels), 2) ! dipole moment <mu_el> = \sum_i -e <x_i>
2808
2810 end function states_elec_calculate_dipole
2811
2812
2813#include "undef.F90"
2814#include "real.F90"
2815#include "states_elec_inc.F90"
2816
2817#include "undef.F90"
2818#include "complex.F90"
2819#include "states_elec_inc.F90"
2820#include "undef.F90"
2821
2822end module states_elec_oct_m
2823
2824
2825!! Local Variables:
2826!! mode: f90
2827!! coding: utf-8
2828!! End:
initialize a batch with existing memory
Definition: batch.F90:294
constant times a vector plus a vector
Definition: lalg_basic.F90:192
scales a vector by a constant
Definition: lalg_basic.F90:168
Prints out to iunit a message in the form: ["InputVariable" = value] where "InputVariable" is given b...
Definition: messages.F90:180
subroutine, public accel_free_buffer(this, async)
Definition: accel.F90:988
pure logical function, public accel_is_enabled()
Definition: accel.F90:412
type(accel_t), public accel
Definition: accel.F90:246
This module implements batches of mesh functions.
Definition: batch.F90:133
This module implements common operations on batches of mesh functions.
Definition: batch_ops.F90:116
This module provides the BLACS processor grid.
subroutine, public blacs_proc_grid_init(this, mpi_grp, procdim)
Initializes a blacs context from an MPI communicator with topological information.
subroutine, public blacs_proc_grid_end(this)
subroutine, public blacs_proc_grid_copy(cin, cout)
Module implementing boundary conditions in Octopus.
Definition: boundaries.F90:122
This module handles the calculation mode.
type(calc_mode_par_t), public calc_mode_par
Singleton instance of parallel calculation mode.
integer, parameter, public p_strategy_states
parallelization in states
This module calculates the derivatives (gradients, Laplacians, etc.) of a function.
subroutine, public zderivatives_grad(der, ff, op_ff, ghost_update, set_bc, to_cartesian)
apply the gradient to a mesh function
subroutine, public zderivatives_lapl(der, ff, op_ff, ghost_update, set_bc, factor)
apply the Laplacian to a mesh function
subroutine, public distributed_end(this)
subroutine, public distributed_init(this, total, comm, tag, scalapack_compat)
Distribute N instances across M processes of communicator comm
subroutine, public distributed_copy(in, out)
Create a copy of a distributed instance.
subroutine, public distributed_init_serial(this, total)
Serial initialization of a distributed instance. The calling process is assigned all total instances,...
integer, parameter, public unpolarized
Parameters...
integer, parameter, public spinors
integer, parameter, public spin_polarized
real(real64), parameter, public m_two
Definition: global.F90:200
real(real64), parameter, public m_zero
Definition: global.F90:198
real(real64), parameter, public m_fourth
Definition: global.F90:207
complex(real64), parameter, public m_zi
Definition: global.F90:212
real(real64), parameter, public m_epsilon
Definition: global.F90:214
type(conf_t), public conf
Global instance of Octopus configuration.
Definition: global.F90:188
real(real64), parameter, public m_half
Definition: global.F90:204
real(real64), parameter, public m_one
Definition: global.F90:199
This module implements the underlying real-space grid.
Definition: grid.F90:117
subroutine, public dgrid_symmetrize_vector_field(gr, field, suppress_warning)
Definition: grid.F90:706
subroutine, public dgrid_symmetrize_scalar_field(gr, field, suppress_warning)
Definition: grid.F90:680
Definition: io.F90:114
logical pure function, public kpoints_point_is_gamma(this, ik)
Definition: kpoints.F90:1733
System information (time, memory, sysname)
Definition: loct.F90:115
This module is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:115
integer pure function, public pad_pow2(size)
create array size, which is padded to powers of 2
Definition: math.F90:933
This module defines functions over batches of mesh functions.
Definition: mesh_batch.F90:116
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_random(mesh, ff, pre_shift, post_shift, seed, normalized)
This subroutine fills a function with random values.
subroutine, public dmf_random(mesh, ff, pre_shift, post_shift, seed, normalized)
This subroutine fills a function with random values.
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_not_implemented(feature, namespace)
Definition: messages.F90:1066
subroutine, public messages_warning(no_lines, all_nodes, namespace)
Definition: messages.F90:523
subroutine, public messages_obsolete_variable(namespace, name, rep)
Definition: messages.F90:998
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:408
subroutine, public messages_input_error(namespace, var, details, row, column)
Definition: messages.F90:689
subroutine, public messages_experimental(name, namespace)
Definition: messages.F90:1038
subroutine, public messages_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:592
general module for modelmb particles
subroutine, public modelmb_particles_end(this)
subroutine, public modelmb_particles_init(this, namespace, space, nst)
==============================================================
subroutine, public modelmb_particles_copy(modelmb_out, modelmb_in)
subroutine mpi_grp_copy(mpi_grp_out, mpi_grp_in)
MPI_THREAD_FUNNELED allows for calls to MPI from an OMP region if the thread is the team master.
Definition: mpi.F90:449
type(mpi_comm), parameter, public mpi_comm_undefined
used to indicate a communicator has not been initialized
Definition: mpi.F90:136
subroutine mpi_grp_init(grp, comm)
Initialize MPI group instance.
Definition: mpi.F90:407
This module handles the communicators for the various parallelization strategies.
Definition: multicomm.F90:145
logical pure function, public multicomm_strategy_is_parallel(mc, level)
Definition: multicomm.F90:726
subroutine, public multicomm_all_pairs_copy(apout, apin)
Definition: multicomm.F90:249
logical pure function, public multicomm_have_slaves(this)
Definition: multicomm.F90:852
logical function, public parse_is_defined(namespace, name)
Definition: parser.F90:461
integer function, public parse_block(namespace, name, blk, check_varinfo_)
Definition: parser.F90:621
/* To use the profiling module, call profiling_in and profiling_out with the label of the region as a...
Definition: profiling.F90:143
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
Definition: profiling.F90:693
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
Definition: profiling.F90:625
subroutine, public smear_find_fermi_energy(this, namespace, eigenvalues, occupations, qtot, nik, nst, kweights)
Definition: smear.F90:378
subroutine, public smear_fill_occupations(this, eigenvalues, occupations, kweights, nik, nst)
Definition: smear.F90:673
subroutine, public smear_copy(to, from)
Definition: smear.F90:347
integer, parameter, public smear_fixed_occ
Definition: smear.F90:174
subroutine, public smear_init(this, namespace, ispin, fixed_occ, integral_occs, kpoints)
Definition: smear.F90:192
logical pure function, public smear_is_semiconducting(this)
Definition: smear.F90:1042
subroutine, public states_set_complex(st)
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_dim_copy(dout, din)
subroutine, public states_elec_dim_end(dim)
This module handles groups of electronic batches and their parallel distribution.
subroutine, public states_elec_group_copy(d, group_in, group_out, copy_data, special, dest_type)
make a copy of a group
subroutine, public states_elec_group_end(this, d)
finalize the local blocks of wave functions and release local arrays
real(real64) function, public states_elec_wfns_memory(st, mesh)
return the memory usage of a states_elec_t object
integer pure function, public states_elec_block_max(st, ib)
return index of last state in block ib
subroutine zstates_elec_generate_random_vector(mesh, st, vector, normalized, reset_seed)
Generate a random vector.
subroutine, public states_elec_null(st)
subroutine, public states_elec_distribute_nodes(st, namespace, mc)
Distribute states over the processes for states parallelization.
subroutine, public states_elec_fermi(st, namespace, mesh, compute_spin)
calculate the Fermi level for the states in this object
logical function, public state_kpt_is_local(st, ist, ik)
check whether a given state (ist, ik) is on the local node
subroutine, public states_elec_calc_quantities(gr, st, kpoints, nlcc, kinetic_energy_density, paramagnetic_current, density_gradient, density_laplacian, gi_kinetic_energy_density, st_end)
calculated selected quantities
real(real64) function, dimension(1:gr%box%dim) states_elec_calculate_dipole(this, gr)
calculate the expectation value of the dipole moment of electrons
subroutine, public states_elec_densities_init(st, gr)
subroutine, public states_elec_end(st)
finalize the states_elec_t object
subroutine zstates_elec_get_state1(st, mesh, idim, ist, iqn, psi)
Write one component (dim) of a wave function into a state_elec_t object.
subroutine, public states_elec_deallocate_wfns(st)
Deallocates the KS wavefunctions defined within a states_elec_t structure.
subroutine, public states_elec_allocate_wfns(st, mesh, wfs_type, skip, packed)
Allocates the KS wavefunctions defined within a states_elec_t structure.
subroutine zstates_elec_get_points1(st, start_point, end_point, iqn, psi)
Returns the value of all the states for given k-point in the range of points [start_point:end_point].
subroutine states_elec_read_initial_occs(st, namespace, excess_charge, kpoints)
Reads from the input file the initial occupations, if the block "Occupations" is present....
subroutine zstates_elec_set_state1(st, mesh, idim, ist, iqn, psi)
get one dimension of local wave function for given k-point and states index from a states_elec_t obje...
real(real64) function, public states_elec_eigenvalues_sum(st, alt_eig)
function to calculate the eigenvalues sum using occupations as weights
integer, parameter, public par_independent
Method used to generate random states.
subroutine, public states_elec_copy(stout, stin, exclude_wfns, exclude_eigenval, special, dest_type, copy_data)
make a (selective) copy of a states_elec_t object
subroutine dstates_elec_generate_random_vector(mesh, st, vector, normalized, reset_seed)
Generate a random vector.
subroutine, public occupied_states(st, namespace, ik, n_filled, n_partially_filled, n_half_filled, filled, partially_filled, half_filled)
return information about occupied orbitals in many-body state
subroutine dstates_elec_get_state2(st, mesh, ist, iqn, psi)
Write a wave function into a state_elec_t object.
subroutine zstates_elec_set_state2(st, mesh, ist, iqn, psi)
get local wave function for given k-point and states index from a states_elec_t object
subroutine zstates_elec_get_state2(st, mesh, ist, iqn, psi)
Write a wave function into a state_elec_t object.
subroutine dstates_elec_get_points1(st, start_point, end_point, iqn, psi)
Returns the value of all the states for given k-point in the range of points [start_point:end_point].
integer pure function, public states_elec_block_size(st, ib)
return number of states in block ib
subroutine, public kpoints_distribute(this, mc)
distribute k-points over the nodes in the corresponding communicator
integer, parameter, public par_dependent
subroutine states_elec_pack(st, copy)
pack the batches in this states object
subroutine, public states_elec_choose_kpoints(st, kpoints, namespace)
double up k-points for SPIN_POLARIZED calculations
subroutine zstates_elec_get_state3(st, mesh, iqn, psi)
get local wave functions for given k-point from a states_elec_t object
logical function, public state_is_local(st, ist)
check whether a given state (ist) is on the local node
subroutine dstates_elec_set_state1(st, mesh, idim, ist, iqn, psi)
get one dimension of local wave function for given k-point and states index from a states_elec_t obje...
subroutine, public states_elec_exec_init(st, namespace, mc)
Further initializations.
subroutine dstates_elec_set_state4(st, mesh, psi)
set all local wave functions in a states_elec_t object
subroutine dstates_elec_set_state3(st, mesh, iqn, psi)
set local wave functions for given k-point in a states_elec_t object
subroutine states_elec_write_info(st, namespace)
write information about the states object
subroutine states_elec_init_block(st, mesh, verbose, skip, packed)
Initializes the data components in st that describe how the states are distributed in blocks:
real(real64) function, dimension(1:3) state_spin(mesh, f1)
calculate the spin vector for a spinor wave function f1
logical pure function, public states_elec_all_states_gpu_packed(st)
return boolean indicating whether all states are packed on the GPU
subroutine states_elec_kpoints_distribution(st)
Assign the start and end indices for states and kpoints, for "st_kpt_mpi_grp" communicator.
subroutine zstates_elec_get_state4(st, mesh, psi)
get all local wave functions from a states_elec_t object
subroutine, public states_elec_init(st, namespace, space, valence_charge, kpoints, calc_mode_id)
Initialize a new 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 dstates_elec_get_state1(st, mesh, idim, ist, iqn, psi)
Write one component (dim) of a wave function into a state_elec_t object.
subroutine dstates_elec_set_state2(st, mesh, ist, iqn, psi)
get local wave function for given k-point and states index from a states_elec_t object
subroutine zstates_elec_set_state4(st, mesh, psi)
set all local wave functions in a states_elec_t object
subroutine states_elec_read_initial_spins(st, namespace)
Reads, if present, the "InitialSpins" block.
subroutine dstates_elec_get_state3(st, mesh, iqn, psi)
get local wave functions for given k-point from a states_elec_t object
subroutine dstates_elec_get_points2(st, start_point, end_point, psi)
Returns the value of all the states in the range of points [start_point:end_point].
integer pure function, public states_elec_block_min(st, ib)
return index of first state in block ib
subroutine states_elec_unpack(st, copy)
unpack the batches in this states object
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...
subroutine dstates_elec_get_state4(st, mesh, psi)
get all local wave functions from a states_elec_t object
subroutine, public states_elec_allocate_current(st, space, mesh)
subroutine, public states_elec_set_zero(st)
Explicitly set all wave functions in the states to zero.
subroutine zstates_elec_set_state3(st, mesh, iqn, psi)
set local wave functions for given k-point in a states_elec_t object
subroutine zstates_elec_get_points2(st, start_point, end_point, psi)
Returns the value of all the states in the range of points [start_point:end_point].
type(type_t), parameter, public type_cmplx
Definition: types.F90:135
type(type_t), parameter, public type_float
Definition: types.F90:134
brief This module defines the class unit_t which is used by the unit_systems_oct_m module.
Definition: unit.F90:132
This module defines the unit system, used for input and output.
type(unit_t), public unit_megabytes
For large amounts of data (natural code units are bytes)
subroutine, public zwfs_elec_init(this, dim, st_start, st_end, np, ik, special, packed)
initialize an empty wfs_elec_t object
Definition: wfs_elec.F90:566
subroutine, public dwfs_elec_init(this, dim, st_start, st_end, np, ik, special, packed)
initialize an empty wfs_elec_t object
Definition: wfs_elec.F90:422
subroutine reduce_all(grp)
subroutine print_kpoints_debug
Class defining batches of mesh functions.
Definition: batch.F90:162
Distribution of N instances over mpi_grpsize processes, for the local rank mpi_grprank....
Extension of space that contains the knowledge of the spin dimension.
Description of the grid, containing information on derivatives, stencil, and symmetries.
Definition: grid.F90:169
Describes mesh distribution to nodes.
Definition: mesh.F90:186
==============================================================
This is defined even when running serial.
Definition: mpi.F90:142
An all-pairs communication schedule for a given group.
Definition: multicomm.F90:238
Stores all communicators and groups.
Definition: multicomm.F90:206
abstract class for states
The states_elec_t class contains all electronic wave functions.
int true(void)