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