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