Octopus
lcao.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
19#include "global.h"
20
21module lcao_oct_m
22 use atom_oct_m
24 use batch_oct_m
25 use blacs_oct_m
28 use comm_oct_m
29 use debug_oct_m
31 use global_oct_m
32 use grid_oct_m
35 use io_oct_m
37 use ions_oct_m
38 use, intrinsic :: iso_fortran_env
41 use lapack_oct_m
42 use loct_oct_m
44 use math_oct_m
45 use mesh_oct_m
48 use mpi_oct_m
51 use parser_oct_m
53 use ps_oct_m
57 use smear_oct_m
58 use space_oct_m
67 use unit_oct_m
69 use v_ks_oct_m
72 use xc_oct_m
73 use xc_sic_oct_m
74
75 implicit none
76
77 private
78 public :: &
79 lcao_t, &
80 lcao_init, &
82 lcao_wf, &
83 lcao_run, &
84 lcao_end, &
87
88 type lcao_t
89 private
90 integer :: mode
91 logical :: complex_ylms
92 logical :: initialized
93 integer :: norbs
94 integer :: maxorbs
95 integer, allocatable :: atom(:)
96 integer, allocatable :: level(:)
97 integer, allocatable :: ddim(:)
98 logical :: alternative
99 logical :: derivative
100 integer, allocatable :: cst(:, :)
101 integer, allocatable :: ck(:, :)
102 real(4), allocatable :: dbuff_single(:, :, :, :)
103 complex(4), allocatable :: zbuff_single(:, :, :, :)
104 real(real64), allocatable :: dbuff(:, :, :, :)
105 complex(real64), allocatable :: zbuff(:, :, :, :)
106 logical :: save_memory
107 logical :: initialized_orbitals
108 real(real64) :: orbital_scale_factor
109
111 logical, allocatable :: is_empty(:)
112 ! !< This occurs in domain parallelization if the atom is not
113 ! !< in the local domain
116 logical :: keep_orb
117 real(real64), allocatable :: radius(:)
118 real(real64) :: lapdist
119 integer :: mult
120 integer :: maxorb
122 integer, allocatable :: basis_atom(:)
123 integer, allocatable :: basis_orb(:)
124 integer, allocatable :: atom_orb_basis(:, :)
125 integer, allocatable :: norb_atom(:)
126 logical :: parallel
127 integer :: lsize(1:2)
128 integer :: nproc(1:2)
129 integer :: myroc(1:2)
130 integer :: desc(1:BLACS_DLEN)
131 logical, allocatable :: calc_atom(:)
132 real(real64) :: diag_tol
133 type(submesh_t), allocatable :: sphere(:)
134 type(batch_t), allocatable :: orbitals(:)
135 logical, allocatable :: is_orbital_initialized(:)
136 end type lcao_t
137
138
139 integer, parameter :: &
140 INITRHO_PARAMAGNETIC = 1, &
142 initrho_random = 3, &
143 initrho_userdef = 77
144
145contains
146
147 ! ---------------------------------------------------------
148 subroutine lcao_init(this, namespace, space, gr, ions, st)
149 type(lcao_t), intent(out) :: this
150 type(namespace_t), intent(in) :: namespace
151 type(electron_space_t), intent(in) :: space
152 type(grid_t), intent(in) :: gr
153 type(ions_t), intent(in) :: ions
154 type(states_elec_t), intent(in) :: st
155
156 integer :: ia, n, iorb, jj, maxj, idim
157 integer :: ii, ll, mm, norbs, ii_tmp
158 integer :: mode_default
159 real(real64) :: max_orb_radius, maxradius
160 integer :: iunit_o
161
162 push_sub(lcao_init)
163
164 this%initialized = .true.
165
166 ! initialization, in case we leave this routine before LCAOAlternative is parsed
167 this%alternative = .false.
168
169 ! The initial LCAO calculation is done by default if we have species representing atoms.
170 ! Otherwise, it is not the default value and has to be enforced in the input file.
171 mode_default = option__lcaostart__lcao_states
172 if (ions%only_user_def) mode_default = option__lcaostart__lcao_none
173
174 !%Variable LCAOStart
175 !%Type integer
176 !%Section SCF::LCAO
177 !%Description
178 !% Before starting a SCF calculation, <tt>Octopus</tt> can perform
179 !% a linear combination of atomic orbitals (LCAO) calculation.
180 !% These can provide <tt>Octopus</tt> with a good set
181 !% of initial wavefunctions and with a new guess for the density.
182 !% (Up to the current version, only a minimal basis set is used.)
183 !% The default is <tt>lcao_states</tt> if at least one species representing an atom is present.
184 !% The default is <tt>lcao_none</tt> if all species are <tt>species_user_defined</tt>,
185 !% <tt>species_charge_density</tt>, <tt>species_from_file</tt>, or <tt>species_jellium_slab</tt>.
186 !%
187 !% The initial guess densities for LCAO are taken from the atomic orbitals for pseudopotential species;
188 !% from the natural charge density for <tt>species_charge_density</tt>, <tt>species_point</tt>,
189 !% <tt>species_jellium</tt>, and <tt>species_jellium_slab</tt>;
190 !% or uniform for <tt>species_full_delta</tt>, <tt>species_full_gaussian</tt>,
191 !% <tt>species_user_defined</tt>, or <tt>species_from_file</tt>.
192 !% Pseudopotential species use the pseudo-wavefunctions as orbitals, full-potential atomic species
193 !% (<tt>species_full_delta</tt> and <tt>species_full_gaussian</tt>) use hydrogenic wavefunctions, and
194 !% others use harmonic-oscillator wavefunctions.
195 !%
196 !% Note: Some pseudopotential files (CPI, FHI for example) do not
197 !% contain full information about the orbitals. In this case,
198 !% Octopus generates the starting density from the normalized
199 !% square root of the local potential. If no orbitals are
200 !% available at all from the pseudopotential files, Octopus will
201 !% not be able to perform an LCAO and the initial states will be
202 !% randomized.
203 !%
204 !%Option lcao_none 0
205 !% Do not perform a LCAO calculation before the SCF cycle. Instead use random wavefunctions.
206 !%Option lcao_states 2
207 !% Do a LCAO calculation before the SCF cycle and use the resulting wavefunctions as
208 !% initial wavefunctions without changing the guess density.
209 !% This will speed up the convergence of the eigensolver during the first SCF iterations.
210 !%Option lcao_full 3
211 !% Do a LCAO calculation before the SCF cycle and use the LCAO wavefunctions to build a new
212 !% guess density and a new KS potential.
213 !% Using the LCAO density as a new guess density may improve the convergence, but can
214 !% also slow it down or yield wrong results (especially for spin-polarized calculations).
215 !%End
216 call parse_variable(namespace, 'LCAOStart', mode_default, this%mode)
217 if (.not. varinfo_valid_option('LCAOStart', this%mode)) call messages_input_error(namespace, 'LCAOStart')
219 call messages_print_var_option('LCAOStart', this%mode, namespace=namespace)
221 if (this%mode == option__lcaostart__lcao_none) then
222 pop_sub(lcao_init)
223 return
224 end if
226 !%Variable LCAOAlternative
227 !%Type logical
228 !%Default false
229 !%Section SCF::LCAO
230 !%Description
231 !% If this variable is set, the LCAO procedure will use an
232 !% alternative (and experimental) implementation. It is faster for
233 !% large systems and parallel in states. It is not working for spinors, however.
234 !%End
235 call parse_variable(namespace, 'LCAOAlternative', .false., this%alternative)
236 ! DAS: For spinors, you will always get magnetization in (1, 0, 0) direction, and the
237 ! eigenvalues will be incorrect. This is due to confusion between spins and spinors in the code.
238 if (st%d%ispin == spinors .and. this%alternative) then
239 message(1) = "LCAOAlternative is not working for spinors."
240 call messages_fatal(1, namespace=namespace)
241 end if
242 if (space%is_periodic() .and. this%alternative) then
243 call messages_experimental("LCAOAlternative in periodic systems", namespace=namespace)
244 ! specifically, if you get the message about submesh radius > box size, results will probably be totally wrong.
245 end if
246
247 !%Variable LCAOComplexYlms
248 !%Type logical
249 !%Default false
250 !%Section SCF::LCAO
251 !%Description
252 !% If set to true, and using complex states, complex spherical harmonics will be used, <i>i.e.</i>
253 !% with <math>e^{\pm i m \phi}</math>.
254 !% If false, real spherical harmonics with <math>\sin(m \phi)</math> or <math>\cos(m \phi)</math> are used.
255 !% This variable will make it more likely to get states that are eigenvectors of the <math>L_z</math>
256 !% operator, with a definite angular momentum.
257 !%End
258
259 if (states_are_complex(st)) then
260 call parse_variable(namespace, 'LCAOComplexYlms', .false., this%complex_ylms)
261 else
262 this%complex_ylms = .false.
263 end if
264
265 !%Variable LCAOSaveMemory
266 !%Type logical
267 !%Default false
268 !%Section SCF::LCAO
269 !%Description
270 !% If set to true, the LCAO will allocate extra memory needed in single precision instead of
271 !% double precision.
272 !%End
273 call parse_variable(namespace, 'LCAOSaveMemory', .false., this%save_memory)
274
275
276 if (debug%info .and. mpi_grp_is_root(mpi_world)) then
277 call io_mkdir('debug/lcao', namespace)
278 iunit_o = io_open('debug/lcao/orbitals', namespace, action='write')
279 write(iunit_o,'(7a6)') 'iorb', 'atom', 'level', 'i', 'l', 'm', 'spin'
280 end if
281
282 if (.not. this%alternative) then
283
284 !%Variable LCAOScaleFactor
285 !%Type float
286 !%Default 1.0
287 !%Section SCF::LCAO
288 !%Description
289 !% The coordinates of the atomic orbitals used by the LCAO
290 !% procedure will be rescaled by the value of this variable. 1.0 means no rescaling.
291 !%End
292 call parse_variable(namespace, 'LCAOScaleFactor', m_one, this%orbital_scale_factor)
293 call messages_print_var_value('LCAOScaleFactor', this%orbital_scale_factor, namespace=namespace)
294
295 !%Variable LCAOMaximumOrbitalRadius
296 !%Type float
297 !%Default 20.0 a.u.
298 !%Section SCF::LCAO
299 !%Description
300 !% The LCAO procedure will ignore orbitals that have an
301 !% extent greater that this value.
302 !%End
303 call parse_variable(namespace, 'LCAOMaximumOrbitalRadius', 20.0_real64, max_orb_radius, unit = units_inp%length)
304 call messages_print_var_value('LCAOMaximumOrbitalRadius', max_orb_radius, units_out%length, namespace=namespace)
305
306 ! count the number of orbitals available
307 maxj = 0
308 this%maxorbs = 0
309 do ia = 1, ions%natoms
310 maxj = max(maxj, ions%atom(ia)%species%get_niwfs())
311 this%maxorbs = this%maxorbs + ions%atom(ia)%species%get_niwfs()
312 end do
313
314 this%maxorbs = this%maxorbs*st%d%dim
315
316 if (this%maxorbs == 0) then
317 call messages_write('The are no atomic orbitals available, cannot do LCAO.')
318 call messages_warning(namespace=namespace)
319 this%mode = option__lcaostart__lcao_none
320 pop_sub(lcao_init)
321 return
322 end if
323
324 ! generate tables to know which indices each atomic orbital has
325
326 safe_allocate( this%atom(1:this%maxorbs))
327 safe_allocate(this%level(1:this%maxorbs))
328 safe_allocate( this%ddim(1:this%maxorbs))
329
330 safe_allocate(this%is_empty(1:this%maxorbs))
331 this%is_empty = .false.
332
333 ! this is determined by the stencil we are using and the spacing
334 this%lapdist = maxval(abs(gr%idx%enlarge)*gr%spacing)
335
336 ! calculate the radius of each orbital
337 safe_allocate(this%radius(1:ions%natoms))
338
339 do ia = 1, ions%natoms
340 norbs = ions%atom(ia)%species%get_niwfs()
341
342 maxradius = m_zero
343 do iorb = 1, norbs
344 call ions%atom(ia)%species%get_iwf_ilm(iorb, 1, ii, ll, mm)
345 ! For all-electron species, we need to use the principal quantum number
346 if(ions%atom(ia)%species%is_full()) call ions%atom(ia)%species%get_iwf_n( iorb, 1, ii)
347 maxradius = max(maxradius, ions%atom(ia)%species%get_iwf_radius( ii, is = 1))
348 end do
349
350 maxradius = min(maxradius, m_two*maxval(gr%box%bounding_box_l(1:space%dim)))
351
352 this%radius(ia) = maxradius
353 end do
354
355
356 ! Each atom provides niwfs pseudo-orbitals (this number is given in
357 ! ions%atom(ia)%species%niwfs for atom number ia). This number is
358 ! actually multiplied by two in case of spin-unrestricted or spinors
359 ! calculations.
360 !
361 ! The pseudo-orbitals are placed in order in the following way (Natoms
362 ! is the total number of atoms).
363 !
364 ! n = 1 => first orbital of atom 1,
365 ! n = 2 => first orbital of atom 2.
366 ! n = 3 => first orbital of atom 3.
367 ! ....
368 ! n = Natoms => first orbital of atom Natoms
369 ! n = Natoms + 1 = > second orbital of atom 1
370 ! ....
371 !
372 ! If at some point in this loop an atom pseudo cannot provide the corresponding
373 ! orbital (because the niws orbitals have been exhausted), it moves on to the following
374 ! atom.
375 !
376 ! In the spinors case, it changes a bit:
377 !
378 ! n = 1 => first spin-up orbital of atom 1, assigned to the spin-up component of the spinor.
379 ! n = 2 => first spin-down orbital of atom 1, assigned to the spin-down component of the spinor.
380 ! n = 3 => first spin-up orbital of atom 2, assigned to the spin-up component of the spinor.
381
382 iorb = 1
383 do jj = 1, maxj
384 do ia = 1, ions%natoms
385 do idim = 1,st%d%dim
386 if (jj > ions%atom(ia)%species%get_niwfs()) cycle
387 call ions%atom(ia)%species%get_iwf_ilm(jj, idim, ii, ll, mm)
388 ! For all-electron species, we need to use the principal quantum number
389 if(ions%atom(ia)%species%is_full()) then
390 ii_tmp = ii
391 call ions%atom(ia)%species%get_iwf_n( ii_tmp, 1, ii)
392 end if
393 if (this%orbital_scale_factor*ions%atom(ia)%species%get_iwf_radius( ii, is = 1) >= max_orb_radius) cycle
394
395 this%atom(iorb) = ia
396 this%level(iorb) = jj
397 this%ddim(iorb) = idim
398
399 if (debug%info .and. mpi_grp_is_root(mpi_world)) then
400 write(iunit_o,'(7i6)') iorb, this%atom(iorb), this%level(iorb), ii, ll, mm, this%ddim(iorb)
401 end if
402
403 iorb = iorb + 1
404 end do
405 end do
406 end do
407
408 if (debug%info .and. mpi_grp_is_root(mpi_world)) then
409 call io_close(iunit_o)
410 end if
411
412 ! some orbitals might have been removed because of their radii
413 if (this%maxorbs /= iorb - 1) then
414 call messages_write('Info: ')
415 call messages_write(this%maxorbs - iorb + 1)
416 call messages_write(' of ')
417 call messages_write(this%maxorbs)
418 call messages_write(' orbitals cannot be used for the LCAO calculation,')
419 call messages_new_line()
420 call messages_write(' their radii exceeds LCAOMaximumOrbitalRadius (')
421 call messages_write(max_orb_radius, units = units_out%length, fmt = '(f6.1)')
422 call messages_write(').')
423 call messages_warning(namespace=namespace)
424
425 this%maxorbs = iorb - 1
426 end if
427
428 if (this%maxorbs < st%nst) then
429 call messages_write('Cannot do LCAO for all states because there are not enough atomic orbitals.')
430 call messages_new_line()
431
432 call messages_write('Required: ')
433 call messages_write(st%nst)
434 call messages_write('. Available: ')
435 call messages_write(this%maxorbs)
436 call messages_write('. ')
437 call messages_write(st%nst - this%maxorbs)
438 call messages_write(' orbitals will be randomized.')
439 call messages_warning(namespace=namespace)
440 end if
441
442 !%Variable LCAODimension
443 !%Type integer
444 !%Section SCF::LCAO
445 !%Description
446 !% (Only applies if <tt>LCAOAlternative = no</tt>.)
447 !% Before starting the SCF cycle, an initial LCAO calculation can be performed
448 !% in order to obtain reasonable initial guesses for spin-orbitals and densities.
449 !% For this purpose, the code calculates a number of atomic orbitals.
450 !% The number available for a species described by a pseudopotential is all the
451 !% orbitals up the maximum angular momentum in the pseudopotential, minus any orbitals that
452 !% are found to be unbound. For non-pseudopotential species, the number is equal to
453 !% twice the valence charge.
454 !% The default dimension for the LCAO basis
455 !% set will be the sum of all these numbers, or twice the number of required orbitals
456 !% for the full calculation, whichever is less.
457 !%
458 !% This dimension however can be changed by making use of this
459 !% variable. Note that <tt>LCAODimension</tt> cannot be smaller than the
460 !% number of orbitals needed in the full calculation -- if
461 !% <tt>LCAODimension</tt> is smaller, it will be silently increased to meet
462 !% this requirement. In the same way, if <tt>LCAODimension</tt> is larger
463 !% than the available number of atomic orbitals, it will be
464 !% reduced. If you want to use the largest possible number, set
465 !% <tt>LCAODimension</tt> to a negative number.
466 !%End
467 call parse_variable(namespace, 'LCAODimension', 0, n)
468
469 if (n > 0 .and. n <= st%nst .and. st%nst <= this%maxorbs) then
470 ! n was made too small
471 this%norbs = st%nst
472 else if (n > st%nst .and. n <= this%maxorbs) then
473 ! n is a reasonable value
474 this%norbs = n
475 else if (n == 0) then
476 ! using the default
477 this%norbs = min(this%maxorbs, 2*st%nst)
478 else
479 ! n was negative, or greater than maxorbs
480 this%norbs = this%maxorbs
481 end if
482
483 assert(this%norbs <= this%maxorbs)
484
485 safe_allocate(this%cst(1:this%norbs, 1:st%d%spin_channels))
486 safe_allocate(this%ck(1:this%norbs, 1:st%d%spin_channels))
487 this%initialized_orbitals = .false.
488 else
489 call lcao2_init()
490 end if
491
492 pop_sub(lcao_init)
493
494 contains
495
496 subroutine lcao2_init()
497 integer :: iatom, iorb, norbs
498 real(real64) :: maxradius
499 integer :: ibasis
500#ifdef HAVE_SCALAPACK
501 integer :: jatom, jorb, jbasis, ilbasis, jlbasis, proc(1:2), info, nbl
502#endif
503 push_sub(lcao_init.lcao2_init)
504
505 call messages_write('Info: Using LCAO alternative implementation.')
506 call messages_info(namespace=namespace)
507
508 call messages_experimental('LCAO alternative implementation', namespace=namespace)
509
510 !%Variable LCAOKeepOrbitals
511 !%Type logical
512 !%Default yes
513 !%Section SCF::LCAO
514 !%Description
515 !% Only applies if <tt>LCAOAlternative = true</tt>.
516 !% If set to yes (the default) Octopus keeps atomic orbitals in
517 !% memory during the LCAO procedure. If set to no, the orbitals
518 !% are generated each time that they are needed, increasing
519 !% computational time but saving memory.
520 !%
521 !% When set to yes, Octopus prints the amount of memory per node
522 !% that is required to store the orbitals.
523 !%
524 !%End
525 call parse_variable(namespace, 'LCAOKeepOrbitals', .true., this%keep_orb)
526
527 !%Variable LCAOExtraOrbitals
528 !%Type logical
529 !%Default false
530 !%Section SCF::LCAO
531 !%Description
532 !% Only applies if <tt>LCAOAlternative = true</tt>, and all species are pseudopotentials.
533 !% (experimental) If this variable is set to yes, the LCAO
534 !% procedure will add an extra set of numerical orbitals (by
535 !% using the derivative of the radial part of the original
536 !% orbitals). Note that this corresponds roughly to adding orbitals
537 !% with higher principal quantum numbers, but the same angular momentum.
538 !% This option may cause problems for unoccupied states since you may miss
539 !% some lower-lying states which correspond to higher angular momenta instead
540 !% of higher principal quantum number.
541 !%End
542 call parse_variable(namespace, 'LCAOExtraOrbitals', .false., this%derivative)
543
544 ! DAS: if you calculate the Na atom this way, spin-polarized, with just one unoccupied state,
545 ! you will obtain states (up and down) which are actually the 10th states if you start with
546 ! random wavefunctions! We really need to implement taking the derivative of the angular part
547 ! instead to be sure of getting decent results!
548
549 if (this%derivative) then
550 call messages_experimental('LCAO extra orbitals', namespace=namespace)
551
552 if (st%nst * st%smear%el_per_state > st%qtot) then
553 message(1) = "Lower-lying empty states may be missed with LCAOExtraOrbitals."
554 call messages_warning(1, namespace=namespace)
555 end if
556 end if
557
558 !%Variable LCAODiagTol
559 !%Type float
560 !%Default 1e-10
561 !%Section SCF::LCAO
562 !%Description
563 !% Only applies if <tt>LCAOAlternative = true</tt>.
564 !% The tolerance for the diagonalization of the LCAO Hamiltonian.
565 !%End
566 call parse_variable(namespace, 'LCAODiagTol', 1e-10_real64, this%diag_tol)
567
568 if (this%derivative) then
569 this%mult = 2
570 else
571 this%mult = 1
572 end if
573
574 safe_allocate(this%sphere(1:ions%natoms))
575 safe_allocate(this%orbitals(1:ions%natoms))
576 safe_allocate(this%is_orbital_initialized(1:ions%natoms))
577 this%is_orbital_initialized = .false.
578
579 safe_allocate(this%norb_atom(1:ions%natoms))
580
581 this%maxorb = 0
582 this%norbs = 0
583 do iatom = 1, ions%natoms
584 this%norb_atom(iatom) = this%mult*ions%atom(iatom)%species%get_niwfs()
585 this%maxorb = max(this%maxorb, ions%atom(iatom)%species%get_niwfs())
586 this%norbs = this%norbs + ions%atom(iatom)%species%get_niwfs()
587 end do
588
589 this%maxorb = this%maxorb*this%mult
590 this%norbs = this%norbs*this%mult
591
592 safe_allocate(this%basis_atom(1:this%norbs))
593 safe_allocate(this%basis_orb(1:this%norbs))
594 safe_allocate(this%atom_orb_basis(1:ions%natoms, 1:this%maxorb))
595
596 ! Initialize the mapping between indices
597
598 ibasis = 0
599 do iatom = 1, ions%natoms
600 norbs = ions%atom(iatom)%species%get_niwfs()
601
602 do iorb = 1, this%mult*norbs
603 ibasis = ibasis + 1
604 this%atom_orb_basis(iatom, iorb) = ibasis
605 this%basis_atom(ibasis) = iatom
606 this%basis_orb(ibasis) = iorb
607
608 ! no stored spin index in alternative mode
609 if (debug%info .and. mpi_grp_is_root(mpi_world)) then
610 call ions%atom(iatom)%species%get_iwf_ilm(iorb, 1, ii, ll, mm)
611 write(iunit_o,'(7i6)') ibasis, iatom, iorb, ii, ll, mm, 1
612 end if
613 end do
614 end do
615
616 if (debug%info .and. mpi_grp_is_root(mpi_world)) then
617 call io_close(iunit_o)
618 end if
619
620 ! this is determined by the stencil we are using and the spacing
621 this%lapdist = maxval(abs(gr%idx%enlarge)*gr%spacing)
622
623 ! calculate the radius of each orbital
624 safe_allocate(this%radius(1:ions%natoms))
625
626 do iatom = 1, ions%natoms
627 norbs = ions%atom(iatom)%species%get_niwfs()
628
629 maxradius = m_zero
630 do iorb = 1, norbs
631 call ions%atom(iatom)%species%get_iwf_ilm(iorb, 1, ii, ll, mm)
632 ! For all-electron species, we need to use the principal quantum number
633 if(ions%atom(iatom)%species%is_full()) call ions%atom(iatom)%species%get_iwf_n( iorb, 1, ii)
634 maxradius = max(maxradius, ions%atom(iatom)%species%get_iwf_radius( ii, is = 1))
635 end do
636
637 if (this%derivative) maxradius = maxradius + this%lapdist
638
639 maxradius = min(maxradius, m_two*maxval(gr%box%bounding_box_l(1:space%dim)))
640
641 this%radius(iatom) = maxradius
642 end do
643
644 safe_allocate(this%calc_atom(1:ions%natoms))
645 this%calc_atom = .true.
646
647 ! initialize parallel data
648#ifndef HAVE_SCALAPACK
649 this%parallel = .false.
650#else
651 this%parallel = (st%parallel_in_states .or. gr%parallel_in_domains) &
652 .and. .not. blacs_proc_grid_null(st%dom_st_proc_grid)
653
654 if (this%parallel) then
655 nbl = min(16, this%norbs)
656
657 ! The size of the distributed matrix in each node
658 this%lsize(1) = max(1, numroc(this%norbs, nbl, st%dom_st_proc_grid%myrow, 0, st%dom_st_proc_grid%nprow))
659 this%lsize(2) = max(1, numroc(this%norbs, nbl, st%dom_st_proc_grid%mycol, 0, st%dom_st_proc_grid%npcol))
660
661 this%nproc(1) = st%dom_st_proc_grid%nprow
662 this%nproc(2) = st%dom_st_proc_grid%npcol
663 this%myroc(1) = st%dom_st_proc_grid%myrow
664 this%myroc(2) = st%dom_st_proc_grid%mycol
665
666 call descinit(this%desc(1), this%norbs, this%norbs, nbl, nbl, 0, 0, &
667 st%dom_st_proc_grid%context, this%lsize(1), info)
668
669 if (info /= 0) then
670 write(message(1), '(a,i6)') 'descinit for BLACS failed with error code ', info
671 call messages_fatal(1, namespace=namespace)
672 end if
673
674 this%calc_atom = .false.
675 do iatom = 1, ions%natoms
676 ibasis = this%atom_orb_basis(iatom, 1)
677
678 do jatom = 1, ions%natoms
679 jbasis = this%atom_orb_basis(jatom, 1)
680
681 do iorb = 1, this%norb_atom(iatom)
682 do jorb = 1, this%norb_atom(jatom)
683 call lcao_local_index(this, ibasis - 1 + iorb, jbasis - 1 + jorb, &
684 ilbasis, jlbasis, proc(1), proc(2))
685
686 this%calc_atom(this%basis_atom(jbasis)) = &
687 this%calc_atom(this%basis_atom(jbasis)) .or. proc(2) == this%myroc(2)
688
689 end do
690 end do
691
692 end do
693 end do
694
695 end if
696#endif
697
698 pop_sub(lcao_init.lcao2_init)
699 end subroutine lcao2_init
700
701 end subroutine lcao_init
702
703
704 ! ---------------------------------------------------------
705 subroutine lcao_run(namespace, space, gr, ions, ext_partners, st, ks, hm, st_start, lmm_r)
706 type(namespace_t), intent(in) :: namespace
707 type(electron_space_t), intent(in) :: space
708 type(grid_t), intent(in) :: gr
709 type(ions_t), intent(in) :: ions
710 type(partner_list_t), intent(in) :: ext_partners
711 type(states_elec_t), intent(inout) :: st
712 type(v_ks_t), intent(inout) :: ks
713 type(hamiltonian_elec_t), intent(inout) :: hm
714 integer, optional, intent(in) :: st_start
715 real(real64), optional, intent(in) :: lmm_r
716
717 type(lcao_t) :: lcao
718 integer :: st_start_random, required_min_nst
719 logical :: lcao_done
720 logical :: is_orbital_dependent
721
722 push_sub(lcao_run)
723
724 if (present(st_start)) then
725 ! If we are doing unocc calculation, do not mess with the correct eigenvalues
726 ! of the occupied states.
727 call v_ks_calc(ks, namespace, space, hm, st, ions, ext_partners, &
728 calc_eigenval=.not. present(st_start), calc_current=.false.)
729
730 assert(st_start >= 1)
731 if (st_start > st%nst) then ! nothing to be done in LCAO
732 pop_sub(lcao_run)
733 return
734 end if
735 end if
736
737 call profiling_in('LCAO_RUN')
738
739 call lcao_init(lcao, namespace, space, gr, ions, st)
740
741 call lcao_init_orbitals(lcao, namespace, st, gr, ions, start = st_start)
742
743 ! By default, we want to use vxc for the LCAO.
744 ! However, we can only do this if vxc depends on the density only
745 ! For cases like MGGA or hybrids, OEP, we cannot do this
746 is_orbital_dependent = (ks%theory_level == hartree .or. ks%theory_level == hartree_fock &
747 .or. (ks%theory_level == kohn_sham_dft .and. xc_is_orbital_dependent(ks%xc)) &
748 .or. (ks%theory_level == generalized_kohn_sham_dft .and. xc_is_orbital_dependent(ks%xc)) &
749 .or. ks%sic%level == sic_pz_oep)
750
751 if (.not. present(st_start)) then
752 call lcao_guess_density(lcao, namespace, st, gr, hm, ions, st%qtot, st%d%ispin, st%d%spin_channels, st%rho)
753
754 if (st%d%ispin > unpolarized) then
755 assert(present(lmm_r))
756 call write_magnetic_moments(gr, st, ions, gr%der%boundaries, lmm_r, namespace=namespace)
757 end if
758
759 ! set up Hamiltonian (we do not call v_ks_h_setup here because we do not want to
760 ! overwrite the guess density)
761 message(1) = 'Info: Setting up Hamiltonian.'
762 call messages_info(1, namespace=namespace)
763
764 ! get the effective potential (we don`t need the eigenvalues yet)
765 call v_ks_calc(ks, namespace, space, hm, st, ions, ext_partners, calc_eigenval=.false., &
766 calc_current=.false., calc_energy=.false., force_semilocal=is_orbital_dependent)
767 ! eigenvalues have nevertheless to be initialized to something
768 ! This value must be larger that the highest eigenvalue from LCAO to get the correct occupations
769 st%eigenval = 1e10_real64
770 end if
771
772 lcao_done = .false.
773
774 ! after initialized, can check that LCAO is possible
775 if (lcao_is_available(lcao)) then
776 lcao_done = .true.
777
778 if (present(st_start)) then
779 write(message(1),'(a,i8,a)') 'Performing LCAO for states ', st_start, ' and above'
780 call messages_info(1, namespace=namespace)
781 end if
782
783 call lcao_wf(lcao, st, gr, ions, hm, namespace, start = st_start)
784
785 ! In some rare cases, like bad pseudopotentials with unbound orbitals,
786 ! we might not have enough orbitals to go up to the Fermi energy
787 ! In this case, we cannot set the eigenvales to a huge value, as
788 ! the smearing will not converge. We set them to zero then
789 select case (st%d%ispin)
790 case (unpolarized)
791 required_min_nst = int(st%qtot/2)
792 case (spin_polarized)
793 required_min_nst = int(st%qtot/2)
794 case (spinors)
795 required_min_nst = int(st%qtot)
796 end select
797 if (st%smear%method /= smear_fixed_occ .and. st%smear%method /= smear_semiconductor) then
798 if (lcao%norbs <= required_min_nst .and. lcao%norbs < st%nst) then
799 st%eigenval(lcao%norbs+1:,:) = m_zero
800 end if
801 end if
802
803 if (.not. present(st_start)) then
804 call states_elec_fermi(st, namespace, gr)
805 call states_elec_write_eigenvalues(min(st%nst, lcao%norbs), st, space, hm%kpoints, namespace=namespace)
806
807 ! Update the density and the Hamiltonian
808 if (lcao%mode == option__lcaostart__lcao_full) then
809 call v_ks_h_setup(namespace, space, gr, ions, ext_partners, st, ks, hm, &
810 calc_eigenval = .false., calc_current=.false.)
811 if (st%d%ispin > unpolarized) then
812 assert(present(lmm_r))
813 call write_magnetic_moments(gr, st, ions, gr%der%boundaries, lmm_r, namespace=namespace)
814 end if
815 end if
816 end if
817 end if
818
819 if (.not. lcao_done .or. lcao%norbs < st%nst) then
820
821 if (lcao_done) then
822 st_start_random = lcao%norbs + 1
823 else
824 st_start_random = 1
825 end if
826 if (present(st_start)) st_start_random = max(st_start, st_start_random)
827
828 if (st_start_random > 1) then
829 write(message(1),'(a,i8,a)') 'Generating random wavefunctions for states ', st_start_random, ' and above'
830 call messages_info(1, namespace=namespace)
831 end if
832
833 ! Randomly generate the initial wavefunctions.
834 call states_elec_generate_random(st, gr, hm%kpoints, ist_start_ = st_start_random, normalized = .false.)
835
836 call messages_write('Orthogonalizing wavefunctions.')
837 call messages_info(namespace=namespace)
838 call states_elec_orthogonalize(st, namespace, gr)
839
840 if (.not. lcao_done) then
841 ! If we are doing unocc calculation, do not mess with the correct eigenvalues and occupations
842 ! of the occupied states.
843 call v_ks_calc(ks, namespace, space, hm, st, ions, ext_partners, &
844 calc_eigenval=.not. present(st_start), calc_current=.false.) ! get potentials
845 if (.not. present(st_start)) then
846 call states_elec_fermi(st, namespace, gr) ! occupations
847 end if
848
849 end if
850
851 else if (present(st_start)) then
852
853 if (st_start > 1) then
854 call messages_write('Orthogonalizing wavefunctions.')
855 call messages_info(namespace=namespace)
856 call states_elec_orthogonalize(st, namespace, gr)
857 end if
858
859 end if
860
861 call lcao_end(lcao)
862
863
864 call profiling_out('LCAO_RUN')
865 pop_sub(lcao_run)
866 end subroutine lcao_run
867
868 ! ---------------------------------------------------------
869 subroutine lcao_end(this)
870 type(lcao_t), intent(inout) :: this
871
872 push_sub(lcao_end)
873
874 safe_deallocate_a(this%calc_atom)
875 safe_deallocate_a(this%norb_atom)
876 safe_deallocate_a(this%basis_atom)
877 safe_deallocate_a(this%basis_orb)
878 safe_deallocate_a(this%atom_orb_basis)
879 safe_deallocate_a(this%radius)
880 safe_deallocate_a(this%sphere)
881 safe_deallocate_a(this%orbitals)
882
883 safe_deallocate_a(this%atom)
884 safe_deallocate_a(this%level)
885 safe_deallocate_a(this%ddim)
886 safe_deallocate_a(this%cst)
887 safe_deallocate_a(this%ck)
888 safe_deallocate_a(this%dbuff_single)
889 safe_deallocate_a(this%zbuff_single)
890 safe_deallocate_a(this%dbuff)
891 safe_deallocate_a(this%zbuff)
892
893 this%initialized = .false.
894 pop_sub(lcao_end)
895 end subroutine lcao_end
896
897
898 ! ---------------------------------------------------------
899 subroutine lcao_wf(this, st, gr, ions, hm, namespace, start)
900 type(lcao_t), intent(inout) :: this
901 type(states_elec_t), intent(inout) :: st
902 type(grid_t), intent(in) :: gr
903 type(ions_t), intent(in) :: ions
904 type(hamiltonian_elec_t), intent(in) :: hm
905 type(namespace_t), intent(in) :: namespace
906 integer, optional, intent(in) :: start
907
908 integer :: start_
909
910 assert(this%initialized)
911
912 call profiling_in("LCAO")
913 push_sub(lcao_wf)
914
915 start_ = 1
916 if (present(start)) start_ = start
917
918 if (this%alternative) then
919 if (states_are_real(st)) then
920 call dlcao_alt_wf(this, st, gr, ions, hm, namespace, start_)
921 else
922 call zlcao_alt_wf(this, st, gr, ions, hm, namespace, start_)
923 end if
924 else
925 if (states_are_real(st)) then
926 call dlcao_wf(this, st, gr, ions, hm, namespace, start_)
927 else
928 call zlcao_wf(this, st, gr, ions, hm, namespace, start_)
929 end if
930 end if
931 pop_sub(lcao_wf)
932 call profiling_out("LCAO")
933 end subroutine lcao_wf
934
935
936 ! ---------------------------------------------------------
937 logical function lcao_is_available(this) result(available)
938 type(lcao_t), intent(in) :: this
939
940 push_sub(lcao_is_available)
941
942 available = this%initialized .and. this%mode /= option__lcaostart__lcao_none
943
944 pop_sub(lcao_is_available)
945 end function lcao_is_available
946
947
948 ! ---------------------------------------------------------
949 integer function lcao_num_orbitals(this) result(norbs)
950 type(lcao_t), intent(in) :: this
951
952 push_sub(lcao_num_orbitals)
953 norbs = this%norbs
954
955 pop_sub(lcao_num_orbitals)
956 end function lcao_num_orbitals
957
958 ! ---------------------------------------------------------
959
960 subroutine lcao_local_index(this, ig, jg, il, jl, prow, pcol)
961 type(lcao_t), intent(in) :: this
962 integer, intent(in) :: ig
963 integer, intent(in) :: jg
964 integer, intent(out) :: il
965 integer, intent(out) :: jl
966 integer, intent(out) :: prow
967 integer, intent(out) :: pcol
968
969 ! no PUSH_SUB, called too often
970#ifdef HAVE_SCALAPACK
971 call infog2l(ig, jg, this%desc(1), this%nproc(1), this%nproc(2), this%myroc(1), this%myroc(2), &
972 il, jl, prow, pcol)
973#else
974 il = ig
975 jl = jg
976 prow = 0
977 pcol = 0
978#endif
979
980 end subroutine lcao_local_index
981
982 ! ---------------------------------------------------------
983
987 subroutine lcao_alt_end_orbital(this, iatom)
988 type(lcao_t), intent(inout) :: this
989 integer, intent(in) :: iatom
990
991 push_sub(lcao_alt_end_orbital)
993 if (this%is_orbital_initialized(iatom)) then
994 call this%orbitals(iatom)%end()
995 this%is_orbital_initialized(iatom) = .false.
996 end if
997
998 pop_sub(lcao_alt_end_orbital)
999
1000 end subroutine lcao_alt_end_orbital
1001
1002 ! ---------------------------------------------------------
1003
1004 subroutine lcao_atom_density(this, st, mesh, ions, iatom, spin_channels, rho)
1005 type(lcao_t), intent(inout) :: this
1006 type(states_elec_t), intent(in) :: st
1007 class(mesh_t), intent(in) :: mesh
1008 type(ions_t), target, intent(in) :: ions
1009 integer, intent(in) :: iatom
1010 integer, intent(in) :: spin_channels
1011 real(real64), intent(inout) :: rho(:, :)
1012
1013 real(real64), allocatable :: dorbital(:, :)
1014 complex(real64), allocatable :: zorbital(:, :)
1015 real(real64), allocatable :: factors(:)
1016 real(real64) :: factor, aa
1017 integer :: iorb, ip, ii, ll, mm, ispin
1018 type(ps_t), pointer :: ps
1019 logical :: use_stored_orbitals
1020
1021 push_sub(lcao_atom_density)
1022
1023 rho = m_zero
1024
1025 use_stored_orbitals = ions%atom(iatom)%species%is_ps() &
1026 .and. states_are_real(st) .and. spin_channels == 1 .and. lcao_is_available(this) &
1027 .and. st%d%dim == 1 .and. .not. ions%space%is_periodic()
1028
1029
1030 ! we can use the orbitals we already have calculated
1031 if (use_stored_orbitals) then
1032 !There is no periodic copies here, so this will not work for periodic systems
1033 assert(.not. ions%space%is_periodic())
1034
1035 select type(spec=>ions%atom(iatom)%species)
1036 class is(pseudopotential_t)
1037 ps => spec%ps
1038 class default
1039 assert(.false.)
1040 end select
1041
1042 if (.not. this%alternative) then
1043
1044 if (states_are_real(st)) then
1045 safe_allocate(dorbital(1:mesh%np, 1:st%d%dim))
1046 else
1047 safe_allocate(zorbital(1:mesh%np, 1:st%d%dim))
1048 end if
1049
1050 do iorb = 1, this%norbs
1051 if (iatom /= this%atom(iorb)) cycle
1052
1053 call ions%atom(iatom)%species%get_iwf_ilm(this%level(iorb), 1, ii, ll, mm)
1054 factor = ps%conf%occ(ii, 1)/(m_two*ll + m_one)
1055
1056 if (states_are_real(st)) then
1057 call dget_ao(this, st, mesh, ions, iorb, 1, dorbital, use_psi = .true.)
1058 !$omp parallel do
1059 do ip = 1, mesh%np
1060 rho(ip, 1) = rho(ip, 1) + factor*dorbital(ip, 1)**2
1061 end do
1062 else
1063 call zget_ao(this, st, mesh, ions, iorb, 1, zorbital, use_psi = .true.)
1064 !$omp parallel do
1065 do ip = 1, mesh%np
1066 rho(ip, 1) = rho(ip, 1) + factor*abs(zorbital(ip, 1))**2
1067 end do
1068 end if
1069
1070 end do
1071
1072 safe_deallocate_a(dorbital)
1073 safe_deallocate_a(zorbital)
1074
1075 else
1076
1077 ! for simplicity, always use real ones here.
1078 call dlcao_alt_get_orbital(this, this%sphere(iatom), ions, 1, iatom, this%norb_atom(iatom))
1079
1080 ! the extra orbitals with the derivative are not relevant here, hence we divide by this%mult
1081 safe_allocate(factors(1:this%norb_atom(iatom)/this%mult))
1082
1083 do iorb = 1, this%norb_atom(iatom)/this%mult
1084 call ions%atom(iatom)%species%get_iwf_ilm(iorb, 1, ii, ll, mm)
1085 factors(iorb) = ps%conf%occ(ii, 1)/(m_two*ll + m_one)
1086 end do
1087
1088 !$omp parallel do private(ip, aa, iorb) if(.not. this%sphere(iatom)%overlap)
1089 do ip = 1, this%sphere(iatom)%np
1090 aa = m_zero
1091 do iorb = 1, this%norb_atom(iatom)/this%mult
1092 aa = aa + factors(iorb)*this%orbitals(iatom)%dff_linear(ip, iorb)**2
1093 end do
1094 rho(this%sphere(iatom)%map(ip), 1) = rho(this%sphere(iatom)%map(ip), 1) + aa
1095 end do
1096
1097 safe_deallocate_a(factors)
1098
1099 end if
1100
1101 else
1102 call species_atom_density(ions%atom(iatom)%species, ions%namespace, ions%space, ions%latt, &
1103 ions%pos(:, iatom), mesh, spin_channels, rho)
1104 end if
1105
1106 ! The above code can sometimes return negative values of the density. Here we avoid introducing
1107 ! them in the calculation of v_s, mixing, ...
1108 do ispin = 1, spin_channels
1109 !$omp parallel do simd
1110 do ip = 1, mesh%np
1111 rho(ip, ispin) = max(rho(ip, ispin), m_zero)
1112 end do
1113 end do
1114
1115 pop_sub(lcao_atom_density)
1116 end subroutine lcao_atom_density
1117
1118 ! ---------------------------------------------------------
1120 subroutine lcao_guess_density(this, namespace, st, gr, hm, ions, qtot, ispin, spin_channels, rho)
1121 type(lcao_t), intent(inout) :: this
1122 type(namespace_t), intent(in) :: namespace
1123 type(states_elec_t), intent(in) :: st
1124 type(grid_t), intent(in) :: gr
1125 type(hamiltonian_elec_t), intent(in) :: hm
1126 type(ions_t), intent(in) :: ions
1127 real(real64), intent(in) :: qtot
1128 integer, intent(in) :: ispin, spin_channels
1129 real(real64), contiguous, intent(out) :: rho(:, :)
1130
1131 integer :: ia, is, idir, gmd_opt, ip
1132 integer(int64), save :: iseed = splitmix64_321
1133 type(block_t) :: blk
1134 real(real64) :: rr, rnd, phi, theta, mag(1:3), lmag, n1, n2,arg
1135 real(real64), allocatable :: atom_rho(:,:)
1136 logical :: parallelized_in_atoms
1137
1138 push_sub(lcao_guess_density)
1139
1140 parallelized_in_atoms = .false.
1141
1142 if (spin_channels == 1) then
1143 gmd_opt = initrho_paramagnetic
1144 else
1145 !%Variable GuessMagnetDensity
1146 !%Type integer
1147 !%Default ferromagnetic
1148 !%Section SCF::LCAO
1149 !%Description
1150 !% The guess density for the SCF cycle is just the sum of all the atomic densities.
1151 !% When performing spin-polarized or non-collinear-spin calculations this option sets
1152 !% the guess magnetization density.
1153 !%
1154 !% For anti-ferromagnetic configurations, the <tt>user_defined</tt> option should be used.
1155 !%
1156 !% Note that if the <tt>paramagnetic</tt> option is used, the final ground state will also be
1157 !% paramagnetic, but the same is not true for the other options.
1158 !%Option paramagnetic 1
1159 !% Magnetization density is zero.
1160 !%Option ferromagnetic 2
1161 !% Magnetization density is the sum of the atomic magnetization densities.
1162 !%Option random 3
1163 !% Each atomic magnetization density is randomly rotated.
1164 !%Option user_defined 77
1165 !% The atomic magnetization densities are rotated so that the magnetization
1166 !% vector has the same direction as a vector provided by the user. In this case,
1167 !% the <tt>AtomsMagnetDirection</tt> block has to be set.
1168 !%End
1169 call parse_variable(namespace, 'GuessMagnetDensity', initrho_ferromagnetic, gmd_opt)
1170 if (.not. varinfo_valid_option('GuessMagnetDensity', gmd_opt)) call messages_input_error(namespace, 'GuessMagnetDensity')
1171 call messages_print_var_option('GuessMagnetDensity', gmd_opt, namespace=namespace)
1172 end if
1173
1174 if (parse_is_defined(namespace, 'GuessMagnetDensity') .and. (hm%theory_level == hartree_fock &
1175 .or. hm%theory_level == generalized_kohn_sham_dft)) then
1176 message(1) = "GuessMagnetDensity cannot be used for Hartree-Fock and generalized Kohn-Sham calculation."
1177 message(2) = "Please perform a LDA or GGA calculation first and restart from this calculation."
1178 call messages_fatal(2, namespace=namespace)
1179 end if
1180
1181 rho = m_zero
1182 select case (gmd_opt)
1183 case (initrho_paramagnetic)
1184 safe_allocate(atom_rho(1:gr%np, 1:spin_channels))
1185
1186 parallelized_in_atoms = .true.
1187
1188 do ia = ions%atoms_dist%start, ions%atoms_dist%end
1189 call lcao_atom_density(this, st, gr, ions, ia, spin_channels, atom_rho)
1190 call lalg_axpy(gr%np, spin_channels, m_one, atom_rho, rho)
1191 end do
1192
1193 if (spin_channels == 2) then
1194 !$omp parallel do
1195 do ip = 1, gr%np
1196 rho(ip, 1) = m_half*(rho(ip, 1) + rho(ip, 2))
1197 rho(ip, 2) = rho(ip, 1)
1198 end do
1199 end if
1200
1202 safe_allocate(atom_rho(1:gr%np, 1:2))
1203
1204 parallelized_in_atoms = .true.
1205
1206 rho = m_zero
1207 do ia = ions%atoms_dist%start, ions%atoms_dist%end
1208 call lcao_atom_density(this, st, gr, ions, ia, 2, atom_rho(1:gr%np, 1:2))
1209 rho(1:gr%np, 1:2) = rho(1:gr%np, 1:2) + atom_rho(1:gr%np, 1:2)
1210 end do
1211
1212 case (initrho_random) ! Randomly oriented spins
1213 safe_allocate(atom_rho(1:gr%np, 1:2))
1214 do ia = 1, ions%natoms
1215 call lcao_atom_density(this, st, gr, ions, ia, 2, atom_rho)
1216
1217 if (ispin == spin_polarized) then
1218 call quickrnd(iseed, rnd)
1219 rnd = rnd - m_half
1220 if (rnd > m_zero) then
1221 rho(1:gr%np, 1:2) = rho(1:gr%np, 1:2) + atom_rho(1:gr%np, 1:2)
1222 else
1223 rho(1:gr%np, 1) = rho(1:gr%np, 1) + atom_rho(1:gr%np, 2)
1224 rho(1:gr%np, 2) = rho(1:gr%np, 2) + atom_rho(1:gr%np, 1)
1225 end if
1226 elseif (ispin == spinors) then
1227 call quickrnd(iseed, phi)
1228 call quickrnd(iseed, theta)
1229 phi = phi*m_two*m_pi
1230 theta = theta*m_pi*m_half
1231
1232 call accumulate_rotated_density(gr, rho, atom_rho, theta, phi)
1233 end if
1234 end do
1235
1236 case (initrho_userdef) ! User-defined
1237
1238 !%Variable AtomsMagnetDirection
1239 !%Type block
1240 !%Section SCF::LCAO
1241 !%Description
1242 !% This option is only used when <tt>GuessMagnetDensity</tt> is
1243 !% set to <tt>user_defined</tt>. It provides a direction for the
1244 !% magnetization vector of each atom when building the guess
1245 !% density. In order to do that, the user should specify the
1246 !% coordinates of a vector that has the desired direction and
1247 !% norm. Note that it is necessary to maintain the ordering in
1248 !% which the species were defined in the coordinates
1249 !% specifications.
1250 !%
1251 !% For spin-polarized calculations, the vectors should have only
1252 !% one component; for non-collinear-spin calculations, they
1253 !% should have three components. If the norm of the vector is greater
1254 !% than the number of valence electrons in the atom, it will be rescaled
1255 !% to this number, which is the maximum possible magnetization.
1256 !%End
1257 if (parse_block(namespace, 'AtomsMagnetDirection', blk) < 0) then
1258 message(1) = "AtomsMagnetDirection block is not defined."
1259 call messages_fatal(1, namespace=namespace)
1260 end if
1261
1262 if (parse_block_n(blk) /= ions%natoms) then
1263 message(1) = "AtomsMagnetDirection block has the wrong number of rows."
1264 call messages_fatal(1, namespace=namespace)
1265 end if
1266
1267 safe_allocate(atom_rho(1:gr%np, 1:2))
1268 do ia = 1, ions%natoms
1269 !Read from AtomsMagnetDirection block
1270 if (ispin == spin_polarized) then
1271 call parse_block_float(blk, ia-1, 0, mag(1))
1272 mag(2:3) = m_zero !Else, this is unitialized and lead to a FPE in the case (lmag > n1+n2)
1273 lmag = abs(mag(1))
1274 elseif (ispin == spinors) then
1275 do idir = 1, 3
1276 call parse_block_float(blk, ia-1, idir-1, mag(idir))
1277 if (abs(mag(idir)) < 1.0e-20_real64) mag(idir) = m_zero
1278 end do
1279 lmag = norm2(mag(1:3))
1280 end if
1281
1282 !Get atomic density
1283 call lcao_atom_density(this, st, gr, ions, ia, 2, atom_rho)
1284
1285 !Scale magnetization density
1286 n1 = dmf_integrate(gr, atom_rho(:, 1))
1287 n2 = dmf_integrate(gr, atom_rho(:, 2))
1288
1289 if (lmag > n1 + n2) then
1290 mag = mag*(n1 + n2)/lmag
1291 lmag = n1 + n2
1292 elseif (abs(lmag) <= m_epsilon) then
1293 if (abs(n1 - n2) <= m_epsilon) then
1294 call lalg_axpy(gr%np, 2, m_one, atom_rho, rho)
1295 else
1296 !$omp parallel do simd
1297 do ip = 1, gr%np
1298 atom_rho(ip, 1) = m_half*(atom_rho(ip, 1) + atom_rho(ip, 2))
1299 rho(ip, 1) = rho(ip, 1) + atom_rho(ip, 1)
1300 rho(ip, 2) = rho(ip, 2) + atom_rho(ip, 1)
1301 end do
1302 end if
1303 cycle
1304 end if
1305
1306 if (.not. is_close(n1 - n2, lmag) .and. abs(n2) > m_epsilon) then
1307 if (n1 - n2 < lmag) then
1308 call lalg_axpy(gr%np, (lmag - n1 + n2)/m_two/n2, atom_rho(:, 2), atom_rho(:, 1))
1309 call lalg_scal(gr%np, (n1 + n2 - lmag)/m_two/n2, atom_rho(:, 2))
1310 elseif (n1 - n2 > lmag) then
1311 call lalg_axpy(gr%np, (n1 - n2 - lmag)/m_two/n1, atom_rho(:, 1), atom_rho(:, 2))
1312 call lalg_scal(gr%np, (n1 + n2 + lmag)/m_two/n1, atom_rho(:, 1))
1313 end if
1314 end if
1315
1316 !Rotate magnetization density
1317 if (ispin == spin_polarized) then
1318
1319 if (mag(1) > m_zero) then
1320 call lalg_axpy(gr%np, 2, m_one, atom_rho, rho)
1321 else
1322 call lalg_axpy(gr%np, m_one, atom_rho(:,2), rho(:,1))
1323 call lalg_axpy(gr%np, m_one, atom_rho(:,1), rho(:,2))
1324 end if
1325
1326 elseif (ispin == spinors) then
1327 assert(lmag > m_zero)
1328 theta = acos(mag(3)/lmag)
1329 if (abs(mag(1)) <= m_epsilon) then
1330 if (abs(mag(2)) <= m_epsilon) then
1331 phi = m_zero
1332 elseif (mag(2) < m_zero) then
1333 phi = m_pi*m_three*m_half
1334 elseif (mag(2) > m_zero) then
1335 phi = m_pi*m_half
1336 end if
1337 else
1338 ! In some rare cases this can be larger than one
1339 arg = mag(1)/sin(theta)/lmag
1340 if (abs(arg) > m_one) arg = sign(m_one, arg)
1341 phi = acos(arg)
1342 if (mag(2) < m_zero) then
1343 phi = m_two*m_pi - phi
1344 end if
1345 end if
1346 theta = m_half*theta
1347 call accumulate_rotated_density(gr, rho, atom_rho, theta, phi)
1348 end if
1349 end do
1350
1351 call parse_block_end(blk)
1352
1353 end select
1354
1355
1356 if (ions%atoms_dist%parallel .and. parallelized_in_atoms) then
1357 ! NOTE: if random or user_defined are made parallelized in atoms, below should be st%d%nspin instead of spin_channels
1358 do is = 1, spin_channels
1359 call lalg_copy(gr%np, rho(:,is), atom_rho(:,1))
1360 call ions%atoms_dist%mpi_grp%allreduce(atom_rho(1, 1), rho(1, is), gr%np, mpi_double_precision, mpi_sum)
1361 end do
1362 end if
1363
1364 ! we now renormalize the density (necessary if we have a charged system)
1365 rr = m_zero
1366 do is = 1, spin_channels
1367 rr = rr + dmf_integrate(gr, rho(:, is), reduce = .false.)
1368 end do
1369 if (gr%parallel_in_domains) then
1370 call gr%allreduce(rr)
1371 end if
1372
1373 write(message(1),'(a,f13.6)')'Info: Unnormalized total charge = ', rr
1374 call messages_info(1, namespace=namespace)
1375
1376 if (abs(rr) > m_epsilon) then ! We only renormalize if the density is not zero
1377 rr = qtot / rr
1378 rho = rr * rho
1379 end if
1380 rr = m_zero
1381 do is = 1, spin_channels
1382 rr = rr + dmf_integrate(gr, rho(:, is), reduce = .false.)
1383 end do
1384 if (gr%parallel_in_domains) then
1385 call gr%allreduce(rr)
1386 end if
1387
1388 write(message(1),'(a,f13.6)')'Info: Renormalized total charge = ', rr
1389 call messages_info(1, namespace=namespace)
1390
1391 if (st%symmetrize_density) then
1392 do is = 1, st%d%nspin
1393 call dgrid_symmetrize_scalar_field(gr, rho(:, is))
1394 end do
1395 end if
1396
1397 safe_deallocate_a(atom_rho)
1398 pop_sub(lcao_guess_density)
1399 end subroutine lcao_guess_density
1400
1401 ! ---------------------------------------------------------
1402 subroutine accumulate_rotated_density(mesh, rho, atom_rho, theta, phi)
1403 class(mesh_t), intent(in) :: mesh
1404 real(real64), intent(inout) :: rho(:,:)
1405 real(real64), intent(in) :: atom_rho(:,:)
1406 real(real64), intent(in) :: theta, phi
1407
1408 integer :: ip
1409
1411
1412 !$omp parallel do simd
1413 do ip = 1, mesh%np
1414 rho(ip, 1) = rho(ip, 1) + cos(theta)**2*atom_rho(ip, 1) + sin(theta)**2*atom_rho(ip, 2)
1415 rho(ip, 2) = rho(ip, 2) + sin(theta)**2*atom_rho(ip, 1) + cos(theta)**2*atom_rho(ip, 2)
1416 rho(ip, 3) = rho(ip, 3) + cos(theta)*sin(theta)*cos(phi)*(atom_rho(ip, 1)-atom_rho(ip, 2))
1417 rho(ip, 4) = rho(ip, 4) - cos(theta)*sin(theta)*sin(phi)*(atom_rho(ip, 1)-atom_rho(ip, 2))
1418 end do
1419
1421 end subroutine accumulate_rotated_density
1422
1423 ! ---------------------------------------------------------
1424
1425 subroutine lcao_init_orbitals(this, namespace, st, gr, ions, start)
1426 type(lcao_t), intent(inout) :: this
1427 type(namespace_t), intent(in) :: namespace
1428 type(states_elec_t), intent(inout) :: st
1429 type(grid_t), intent(in) :: gr
1430 type(ions_t), intent(in) :: ions
1431 integer, optional, intent(in) :: start
1432
1433 if (.not. lcao_is_available(this)) return
1434
1435 push_sub(lcao_init_orbitals)
1436
1437 if (.not. this%alternative) then
1438 if (states_are_real(st)) then
1439 call dinit_orbitals(this, namespace, st, gr, ions, start)
1440 else
1441 call zinit_orbitals(this, namespace, st, gr, ions, start)
1442 end if
1443 else
1444 if (states_are_real(st)) then
1445 call dlcao_alt_init_orbitals(this, namespace, st, gr, ions, start)
1446 else
1447 call zlcao_alt_init_orbitals(this, namespace, st, gr, ions, start)
1448 end if
1449
1450 end if
1451
1452 pop_sub(lcao_init_orbitals)
1453 end subroutine lcao_init_orbitals
1454
1455#include "undef.F90"
1456#include "real.F90"
1457#include "lcao_inc.F90"
1458
1459#include "undef.F90"
1460#include "complex.F90"
1461#include "lcao_inc.F90"
1462
1463
1464end module lcao_oct_m
1465
1466!! Local Variables:
1467!! mode: f90
1468!! coding: utf-8
1469!! End:
subroutine info()
Definition: em_resp.F90:1096
constant times a vector plus a vector
Definition: lalg_basic.F90:170
Copies a vector x, to a vector y.
Definition: lalg_basic.F90:185
scales a vector by a constant
Definition: lalg_basic.F90:156
Prints out to iunit a message in the form: ["InputVariable" = value] where "InputVariable" is given b...
Definition: messages.F90:180
double acos(double __x) __attribute__((__nothrow__
double sin(double __x) __attribute__((__nothrow__
double cos(double __x) __attribute__((__nothrow__
subroutine lcao2_init()
Definition: lcao.F90:590
This module implements batches of mesh functions.
Definition: batch.F90:133
This module contains interfaces for BLACS routines Interfaces are from http:
Definition: blacs.F90:27
This module provides the BLACS processor grid.
logical pure function, public blacs_proc_grid_null(this)
Module implementing boundary conditions in Octopus.
Definition: boundaries.F90:122
type(debug_t), save, public debug
Definition: debug.F90:154
integer, parameter, public unpolarized
Parameters...
integer, parameter, public spinors
integer, parameter, public spin_polarized
real(real64), parameter, public m_two
Definition: global.F90:189
real(real64), parameter, public m_zero
Definition: global.F90:187
real(real64), parameter, public m_pi
some mathematical constants
Definition: global.F90:185
real(real64), parameter, public m_epsilon
Definition: global.F90:203
real(real64), parameter, public m_half
Definition: global.F90:193
real(real64), parameter, public m_one
Definition: global.F90:188
real(real64), parameter, public m_three
Definition: global.F90:190
This module implements the underlying real-space grid.
Definition: grid.F90:117
subroutine, public dgrid_symmetrize_scalar_field(gr, field, suppress_warning)
Definition: grid.F90:661
integer, parameter, public generalized_kohn_sham_dft
integer, parameter, public hartree
integer, parameter, public hartree_fock
integer, parameter, public kohn_sham_dft
This module defines classes and functions for interaction partners.
Definition: io.F90:114
subroutine, public io_close(iunit, grp)
Definition: io.F90:468
subroutine, public io_mkdir(fname, namespace, parents)
Definition: io.F90:354
integer function, public io_open(file, namespace, action, status, form, position, die, recl, grp)
Definition: io.F90:395
This module contains interfaces for LAPACK routines.
Definition: lapack.F90:118
subroutine, public lcao_init_orbitals(this, namespace, st, gr, ions, start)
Definition: lcao.F90:1519
subroutine, public lcao_wf(this, st, gr, ions, hm, namespace, start)
Definition: lcao.F90:993
subroutine zlcao_alt_init_orbitals(this, namespace, st, gr, ions, start)
Definition: lcao.F90:3362
subroutine zget_ao(this, st, mesh, ions, iorb, ispin, ao, use_psi)
Definition: lcao.F90:3330
subroutine, public lcao_end(this)
Definition: lcao.F90:963
subroutine, public lcao_run(namespace, space, gr, ions, ext_partners, st, ks, hm, st_start, lmm_r)
Definition: lcao.F90:799
subroutine zlcao_alt_wf(this, st, gr, ions, hm, namespace, start)
The alternative implementation.
Definition: lcao.F90:3415
subroutine dlcao_wf(this, st, gr, ions, hm, namespace, start)
Definition: lcao.F90:1689
subroutine dlcao_alt_init_orbitals(this, namespace, st, gr, ions, start)
Definition: lcao.F90:2086
subroutine, public lcao_init(this, namespace, space, gr, ions, st)
Definition: lcao.F90:242
subroutine zlcao_wf(this, st, gr, ions, hm, namespace, start)
Definition: lcao.F90:2957
subroutine dlcao_alt_wf(this, st, gr, ions, hm, namespace, start)
The alternative implementation.
Definition: lcao.F90:2139
subroutine lcao_guess_density(this, namespace, st, gr, hm, ions, qtot, ispin, spin_channels, rho)
builds a density which is the sum of the atomic densities
Definition: lcao.F90:1214
integer, parameter initrho_userdef
Definition: lcao.F90:232
integer, parameter initrho_random
Definition: lcao.F90:232
subroutine lcao_alt_end_orbital(this, iatom)
This function deallocates a set of an atomic orbitals for an atom. It can be called when the batch is...
Definition: lcao.F90:1081
subroutine lcao_local_index(this, ig, jg, il, jl, prow, pcol)
Definition: lcao.F90:1054
integer function, public lcao_num_orbitals(this)
Definition: lcao.F90:1043
subroutine dinit_orbitals(this, namespace, st, gr, ions, start)
Definition: lcao.F90:1945
subroutine dget_ao(this, st, mesh, ions, iorb, ispin, ao, use_psi)
Definition: lcao.F90:2054
subroutine dlcao_alt_get_orbital(this, sphere, ions, ispin, iatom, norbs)
This function generates the set of an atomic orbitals for an atom and stores it in the batch orbitalb...
Definition: lcao.F90:2761
subroutine zinit_orbitals(this, namespace, st, gr, ions, start)
Definition: lcao.F90:3221
subroutine lcao_atom_density(this, st, mesh, ions, iatom, spin_channels, rho)
Definition: lcao.F90:1098
subroutine accumulate_rotated_density(mesh, rho, atom_rho, theta, phi)
Definition: lcao.F90:1496
integer, parameter initrho_ferromagnetic
Definition: lcao.F90:232
logical function, public lcao_is_available(this)
Definition: lcao.F90:1031
subroutine, public write_magnetic_moments(mesh, st, ions, boundaries, lmm_r, iunit, namespace)
Definition: magnetic.F90:211
This module is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:115
This module defines various routines, operating on mesh functions.
real(real64) function, public dmf_integrate(mesh, ff, mask, reduce)
Integrate a function on the mesh.
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:118
subroutine, public messages_warning(no_lines, all_nodes, namespace)
Definition: messages.F90:543
subroutine, public messages_info(no_lines, iunit, verbose_limit, stress, all_nodes, namespace)
Definition: messages.F90:624
subroutine, public messages_new_line()
Definition: messages.F90:1146
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:160
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:420
subroutine, public messages_input_error(namespace, var, details, row, column)
Definition: messages.F90:723
subroutine, public messages_experimental(name, namespace)
Definition: messages.F90:1097
logical function mpi_grp_is_root(grp)
Is the current MPI process of grpcomm, root.
Definition: mpi.F90:430
type(mpi_grp_t), public mpi_world
Definition: mpi.F90:266
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
Definition: ps.F90:114
integer(int64), parameter, public splitmix64_321
Definition: quickrnd.F90:134
This module contains interfaces for ScaLAPACK routines Interfaces are from http:
Definition: scalapack.F90:131
integer, parameter, public smear_semiconductor
Definition: smear.F90:171
integer, parameter, public smear_fixed_occ
Definition: smear.F90:171
subroutine, public species_atom_density(species, namespace, space, latt, pos, mesh, spin_channels, rho)
pure logical function, public states_are_complex(st)
pure logical function, public states_are_real(st)
subroutine, public states_elec_orthogonalize(st, namespace, mesh)
Orthonormalizes nst orbitals in mesh (honours state parallelization).
This module defines routines to write information about states.
subroutine, public states_elec_write_eigenvalues(nst, st, space, kpoints, error, st_start, compact, iunit, namespace)
write the eigenvalues for some states to a file.
subroutine, public states_elec_fermi(st, namespace, mesh, compute_spin)
calculate the Fermi level for the states in this object
subroutine, public states_elec_generate_random(st, mesh, kpoints, ist_start_, ist_end_, ikpt_start_, ikpt_end_, normalized)
randomize states
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_system_t), public units_out
type(unit_system_t), public units_inp
the units systems for reading and writing
subroutine, public v_ks_calc(ks, namespace, space, hm, st, ions, ext_partners, calc_eigenval, time, calc_energy, calc_current, force_semilocal)
Definition: v_ks.F90:732
subroutine, public v_ks_h_setup(namespace, space, gr, ions, ext_partners, st, ks, hm, calc_eigenval, calc_current)
Definition: v_ks.F90:679
Definition: xc.F90:114
logical pure function, public xc_is_orbital_dependent(xcs)
Is the xc family orbital dependent.
Definition: xc.F90:541
integer, parameter, public sic_pz_oep
Perdew-Zunger SIC (OEP way)
Definition: xc_sic.F90:148
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
The states_elec_t class contains all electronic wave functions.
int true(void)