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