Octopus
hamiltonian_elec.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2020 M. Marques, A. Castro, A. Rubio, G. Bertsch,
2!! N. Tancogne-Dejean, M. Lueders
3!!
4!! This program is free software; you can redistribute it and/or modify
5!! it under the terms of the GNU General Public License as published by
6!! the Free Software Foundation; either version 2, or (at your option)
7!! any later version.
8!!
9!! This program is distributed in the hope that it will be useful,
10!! but WITHOUT ANY WARRANTY; without even the implied warranty of
11!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12!! GNU General Public License for more details.
13!!
14!! You should have received a copy of the GNU General Public License
15!! along with this program; if not, write to the Free Software
16!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17!! 02110-1301, USA.
18!!
19
20#include "global.h"
21
24 use accel_oct_m
26 use batch_oct_m
29 use debug_oct_m
32 use energy_oct_m
37 use epot_oct_m
40 use global_oct_m
41 use grid_oct_m
45 use io_oct_m
46 use ions_oct_m
47 use kick_oct_m
48 use, intrinsic :: iso_fortran_env
52 use lasers_oct_m
54 use lda_u_oct_m
57 use math_oct_m
58 use mesh_oct_m
61 use mpi_oct_m
65 use nlcc_oct_m
69 use parser_oct_m
74 use pcm_oct_m
75 use phase_oct_m
78 use space_oct_m
86 use types_oct_m
87 use unit_oct_m
91 use xc_oct_m
92 use xc_cam_oct_m
93 use xc_f03_lib_m
97 use zora_oct_m
98
99 implicit none
100
101 private
102 public :: &
121 dvmask, &
122 zvmask, &
137
138
139 type, extends(hamiltonian_abst_t) :: hamiltonian_elec_t
140 ! Components are public by default
141
144 type(space_t), private :: space
145 type(states_elec_dim_t) :: d
146 type(hamiltonian_elec_base_t) :: hm_base
147 type(phase_t) :: phase
148 type(energy_t), allocatable :: energy
149 type(absorbing_boundaries_t) :: abs_boundaries
150 type(ks_potential_t) :: ks_pot
151 real(real64), allocatable :: vberry(:,:)
152
153 type(derivatives_t), pointer, private :: der
154
155 type(nonlocal_pseudopotential_t) :: vnl
156 type(nonlocal_pseudopotential_t) :: aug_overlap
157 type(nonlocal_pseudopotential_t) :: aug_inverse_overlap
158
159 type(ions_t), pointer :: ions
160 logical, private :: owns_ions = .false.
161 logical, private :: is_copy_snapshot = .false.
162 real(real64) :: exx_coef
163
164 class(poisson_t), allocatable :: psolver
165
167 logical :: self_induced_magnetic
168 real(real64), allocatable :: a_ind(:, :)
169 real(real64), allocatable :: b_ind(:, :)
170
171 integer :: theory_level
172 type(xc_t), pointer :: xc
173 type(xc_photons_t), pointer :: xc_photons
174
175 class(epot_t), allocatable :: ep
176 class(pcm_t), allocatable :: pcm
177
179 logical, private :: adjoint
180
182 real(real64), private :: mass
183
185 logical, private :: inh_term
186 class(states_elec_t), allocatable :: inh_st
187
190 type(oct_exchange_t) :: oct_exchange
191
192 class(scissor_t), allocatable :: scissor
193
194 real(real64) :: current_time
195 logical, private :: is_applied_packed
196
198 class(lda_u_t), allocatable :: lda_u
199 integer :: lda_u_level
200
201 logical, public :: time_zero
202
203 class(exchange_operator_t), allocatable, public :: exxop
204
205 type(kpoints_t), pointer, public :: kpoints => null()
206
207 type(partner_list_t) :: external_potentials
208 real(real64), allocatable, public :: v_ext_pot(:)
209 real(real64), allocatable, public :: v_static(:)
210
211 type(ion_electron_local_potential_t) :: v_ie_loc
212 type(nlcc_t) :: nlcc
213
214 type(magnetic_constrain_t) :: magnetic_constrain
215
217 type(kick_t) :: kick
220 type(mxll_coupling_t) :: mxll
221 type(zora_t), pointer :: zora => null()
223 contains
224 procedure :: update => hamiltonian_elec_update
225 procedure :: apply_packed => hamiltonian_elec_apply_packed
226 procedure :: update_span => hamiltonian_elec_span
227 procedure :: dapply => dhamiltonian_elec_apply
228 procedure :: zapply => zhamiltonian_elec_apply
230 procedure :: needs_mgga_term => hamiltonian_elec_needs_mgga_term
231 procedure :: set_mass => hamiltonian_elec_set_mass
234 integer, public, parameter :: &
235 length = 1, &
237
238
239contains
241 ! ---------------------------------------------------------
242 subroutine hamiltonian_elec_init(hm, namespace, space, gr, ions, ext_partners, st, theory_level, xc, &
243 mc, kpoints, need_exchange, xc_photons)
244 type(hamiltonian_elec_t), target, intent(inout) :: hm
245 type(namespace_t), intent(in) :: namespace
246 class(space_t), intent(in) :: space
247 type(grid_t), target, intent(inout) :: gr
248 type(ions_t), target, intent(inout) :: ions
249 type(partner_list_t), intent(inout) :: ext_partners
250 type(states_elec_t), target, intent(inout) :: st
251 integer, intent(in) :: theory_level
252 type(xc_t), target, intent(in) :: xc
253 type(multicomm_t), intent(in) :: mc
254 type(kpoints_t), target, intent(in) :: kpoints
255 logical, optional, intent(in) :: need_exchange
256 type(xc_photons_t), optional, target, intent(in) :: xc_photons
259 logical :: need_exchange_
260 real(real64) :: rashba_coupling
261
263
264 ! class(...), allocatable members: see electrons_t
265 safe_allocate(hm%lda_u)
266 safe_allocate(hm%exxop)
267 safe_allocate(hm%psolver)
268 safe_allocate(hm%scissor)
269 safe_allocate(hm%inh_st)
270 safe_allocate(hm%ep)
271 safe_allocate(hm%pcm)
272 safe_allocate(hm%lda_u%basis)
273 call profiling_in('HAMILTONIAN_ELEC_INIT')
274
275 ! make a couple of local copies
276 hm%space = space
277 hm%theory_level = theory_level
278 call states_elec_dim_copy(hm%d, st%d)
280 hm%kpoints => kpoints
282 !%Variable ParticleMass
283 !%Type float
284 !%Default 1.0
285 !%Section Hamiltonian
286 !%Description
287 !% It is possible to make calculations for a particle with a mass
288 !% different from one (atomic unit of mass, or mass of the electron).
289 !% This is useful to describe non-electronic systems, or for
290 !% esoteric purposes.
291 !%End
292 call parse_variable(namespace, 'ParticleMass', m_one, hm%mass)
294 !%Variable RashbaSpinOrbitCoupling
295 !%Type float
296 !%Default 0.0
297 !%Section Hamiltonian
298 !%Description
299 !% (Experimental.) For systems described in 2D (electrons confined to 2D in semiconductor structures), one
300 !% may add the Bychkov-Rashba spin-orbit coupling term [Bychkov and Rashba, <i>J. Phys. C: Solid
301 !% State Phys.</i> <b>17</b>, 6031 (1984)]. This variable determines the strength
302 !% of this perturbation, and has dimensions of energy times length.
303 !%End
304 call parse_variable(namespace, 'RashbaSpinOrbitCoupling', m_zero, rashba_coupling, units_inp%energy*units_inp%length)
305 if (parse_is_defined(namespace, 'RashbaSpinOrbitCoupling')) then
306 if (space%dim /= 2) then
307 write(message(1),'(a)') 'Rashba spin-orbit coupling can only be used for two-dimensional systems.'
308 call messages_fatal(1, namespace=namespace)
309 end if
310 call messages_experimental('RashbaSpinOrbitCoupling', namespace=namespace)
311 end if
312
313 call hm%hm_base%init(hm%d%nspin, hm%mass, rashba_coupling)
314 call hm%vnl%init()
315 call hm%aug_overlap%init()
316 call hm%aug_inverse_overlap%init()
317
318 assert(associated(gr%der%lapl))
319 hm%hm_base%kinetic => gr%der%lapl
320
321 safe_allocate(hm%energy)
322
323 !Keep pointers to derivatives, geometry and xc
324 hm%der => gr%der
325 hm%ions => ions
326 hm%owns_ions = .false.
327 hm%is_copy_snapshot = .false.
328 hm%xc => xc
329
330 if(present(xc_photons)) then
331 hm%xc_photons => xc_photons
332 else
333 hm%xc_photons => null()
334 end if
335
336 ! allocate potentials and density of the cores
337 ! In the case of spinors, vxc_11 = hm%vxc(:, 1), vxc_22 = hm%vxc(:, 2), Re(vxc_12) = hm%vxc(:. 3);
338 ! Im(vxc_12) = hm%vxc(:, 4)
339 call hm%ks_pot%init(gr%der, gr%np, gr%np_part, hm%d%nspin, hm%theory_level, family_is_mgga_with_exc(hm%xc))
340
341 !Initialize Poisson solvers
342 call poisson_init(hm%psolver, namespace, space, gr%der, mc, gr%stencil, st%qtot)
343
344 ! Initialize external potential
345 call epot_init(hm%ep, namespace, gr, hm%ions, hm%psolver, hm%d%ispin, hm%xc%family, hm%kpoints)
346 st%requires_generalized_overlap = hm%ep%requires_generalized_overlap
347 ! Ultrasoft/PAW (generalized overlap) is not implemented for curvilinear grids
348 ! (the radial-grid overlap and the augmentation-density quadrature assume a
349 ! uniform Cartesian mesh) nor on accelerators (the overlap, inverse overlap and
350 ! augmentation-density kernels have no GPU path).
351 if (hm%ep%requires_generalized_overlap) then
352 if (gr%use_curvilinear) then
353 call messages_not_implemented("ultrasoft/PAW generalized overlap with curvilinear coordinates", &
354 namespace=namespace)
355 end if
356 if (accel_is_enabled()) then
357 call messages_not_implemented("ultrasoft/PAW generalized overlap on GPU/accelerators", &
358 namespace=namespace)
359 end if
360
361 !%Variable USPPSaveMemory
362 !%Type logical
363 !%Default no
364 !%Section Hamiltonian
365 !%Description
366 !% For fully-relativistic (spin-orbit) ultrasoft pseudopotentials, the real spherical
367 !% harmonics <math>Y_{LM}</math> on the augmentation spheres are, by default, precomputed
368 !% once and cached in the projector matrices for the <math>D_{ij}</math> screening, which
369 !% stores an extra <tt>n_points * (l_max+1)^2</tt> reals per atom. Set this to
370 !% <tt>yes</tt> to instead recompute them on every call, trading speed for a smaller
371 !% memory footprint. The augmentation functions <math>Q_{ij}^L(r) Y_{LM}</math> used
372 !% elsewhere are always stored and are not affected by this variable.
373 !%End
374 call parse_variable(namespace, 'USPPSaveMemory', .false., hm%vnl%uspp_save_memory)
375 hm%aug_overlap%uspp_save_memory = hm%vnl%uspp_save_memory
376 hm%aug_inverse_overlap%uspp_save_memory = hm%vnl%uspp_save_memory
377 end if
378 call kick_init(hm%kick, namespace, space, hm%kpoints, hm%d%ispin)
379
380 hm%zora => zora_t(namespace, hm%der, hm%d, hm%ep, hm%mass)
381
382 !Temporary construction of the ion-electron interactions
383 call hm%v_ie_loc%init(gr, hm%psolver, hm%ions, namespace)
384 if (hm%ep%nlcc) then
385 call hm%nlcc%init(gr, hm%ions, hm%ep%dgrid)
386 safe_allocate(st%rho_core(1:gr%np))
387 st%rho_core(:) = m_zero
388 end if
389
390 !Static magnetic field or rashba spin-orbit interaction requires complex wavefunctions
391 if (parse_is_defined(namespace, 'StaticMagneticField') .or. list_has_gauge_field(ext_partners) .or. &
392 parse_is_defined(namespace, 'RashbaSpinOrbitCoupling')) then
393 call states_set_complex(st)
394 end if
395
396 !%Variable CalculateSelfInducedMagneticField
397 !%Type logical
398 !%Default no
399 !%Section Hamiltonian
400 !%Description
401 !% The existence of an electronic current implies the creation of a self-induced magnetic
402 !% field, which may in turn back-react on the system. Of course, a fully consistent treatment
403 !% of this kind of effect should be done in QED theory, but we will attempt a first
404 !% approximation to the problem by considering the lowest-order relativistic terms
405 !% plugged into the normal Hamiltonian equations (spin-other-orbit coupling terms, etc.).
406 !% For the moment being, none of this is done, but a first step is taken by calculating
407 !% the induced magnetic field of a system that has a current, by considering the magnetostatic
408 !% approximation and Biot-Savart law:
409 !%
410 !% <math> \nabla^2 \vec{A} + 4\pi\alpha \vec{J} = 0</math>
411 !%
412 !% <math> \vec{B} = \vec{\nabla} \times \vec{A}</math>
413 !%
414 !% If <tt>CalculateSelfInducedMagneticField</tt> is set to yes, this <i>B</i> field is
415 !% calculated at the end of a <tt>gs</tt> calculation (nothing is done -- yet -- in the <tt>td</tt>case)
416 !% and printed out, if the <tt>Output</tt> variable contains the <tt>potential</tt> keyword (the prefix
417 !% of the output files is <tt>Bind</tt>).
418 !%End
419 call parse_variable(namespace, 'CalculateSelfInducedMagneticField', .false., hm%self_induced_magnetic)
420 if (hm%self_induced_magnetic) then
421 safe_allocate(hm%a_ind(1:gr%np_part, 1:space%dim))
422 safe_allocate(hm%b_ind(1:gr%np_part, 1:space%dim))
423
424 !(for dim = we could save some memory, but it is better to keep it simple)
425 end if
426
427 ! Absorbing boundaries
428 call absorbing_boundaries_init(hm%abs_boundaries, namespace, space, gr)
429
430 hm%inh_term = .false.
431 call oct_exchange_remove(hm%oct_exchange)
432
433 hm%adjoint = .false.
434
435 call hm%phase%init(gr, hm%d%kpt, hm%kpoints, st%d, space)
436
437 !%Variable DFTULevel
438 !%Type integer
439 !%Default no
440 !%Section Hamiltonian::XC
441 !%Description
442 !% This variable selects which DFT+U expression is added to the Hamiltonian.
443 !%Option dft_u_none 0
444 !% No +U term is not applied.
445 !%Option dft_u_empirical 1
446 !% An empiricial Hubbard U is added on the orbitals specified in the block species
447 !% with hubbard_l and hubbard_u
448 !%Option dft_u_acbn0 2
449 !% Octopus determines the effective U term using the
450 !% ACBN0 functional as defined in PRX 5, 011006 (2015)
451 !%End
452 call parse_variable(namespace, 'DFTULevel', dft_u_none, hm%lda_u_level)
453 call messages_print_var_option('DFTULevel', hm%lda_u_level, namespace=namespace)
454 if (hm%lda_u_level /= dft_u_none) then
455 call lda_u_init(hm%lda_u, namespace, space, hm%lda_u_level, gr, ions, st, mc, hm%kpoints)
456
457 !In the present implementation of DFT+U, in case of spinors, we have off-diagonal terms
458 !in spin space which break the assumption of the generalized Bloch theorem
459 if (kick_get_type(hm%kick) == kick_magnon_mode .and. gr%der%boundaries%spiral) then
460 call messages_not_implemented("DFT+U with generalized Bloch theorem and magnon kick", namespace=namespace)
461 end if
462
463 ! We rebuild the phase for the orbital projection, similarly to the one of the pseudopotentials
464 if(hm%lda_u_level /= dft_u_none .and. hm%phase%is_allocated()) then
465 call lda_u_build_phase_correction(hm%lda_u, space, hm%d, gr%der%boundaries, namespace, hm%kpoints)
466 end if
467 end if
468
469 !%Variable HamiltonianApplyPacked
470 !%Type logical
471 !%Default yes
472 !%Section Execution::Optimization
473 !%Description
474 !% If set to yes (the default), Octopus will 'pack' the
475 !% wave-functions when operating with them. This might involve some
476 !% additional copying but makes operations more efficient.
477 !% See also the related <tt>StatesPack</tt> variable.
478 !%End
479 call parse_variable(namespace, 'HamiltonianApplyPacked', .true., hm%is_applied_packed)
480
481 if (hm%theory_level == hartree_fock .and. st%parallel_in_states) then
482 call messages_experimental('Hartree-Fock parallel in states', namespace=namespace)
483 end if
484
485 if (hm%theory_level == generalized_kohn_sham_dft .and. family_is_hybrid(hm%xc) &
486 .and. st%parallel_in_states) then
487 call messages_experimental('Hybrid functionals parallel in states', namespace=namespace)
488 end if
489
490 !%Variable TimeZero
491 !%Type logical
492 !%Default no
493 !%Section Hamiltonian
494 !%Description
495 !% (Experimental) If set to yes, the ground state and other time
496 !% dependent calculation will assume that they are done at time
497 !% zero, so that all time depedent field at that time will be
498 !% included.
499 !%End
500 call parse_variable(namespace, 'TimeZero', .false., hm%time_zero)
501 if (hm%time_zero) call messages_experimental('TimeZero', namespace=namespace)
502
503 !Cam parameters are irrelevant here and are updated later
504 need_exchange_ = optional_default(need_exchange, .false.)
505 if (hm%xc%compute_exchange(hm%theory_level) .or. need_exchange_) then
506 !We test Slater before OEP, as Slater is treated as OEP for the moment....
507 if (hm%xc%functional(func_x,1)%id == xc_oep_x_slater) then
508 call exchange_operator_init(hm%exxop, namespace, space, st, gr%der, mc, gr%stencil, &
509 hm%kpoints, cam_exact_exchange)
510 else if (bitand(hm%xc%family, xc_family_oep) /= 0 .or. hm%theory_level == rdmft) then
511 call exchange_operator_init(hm%exxop, namespace, space, st, gr%der, mc, gr%stencil, &
512 hm%kpoints, hm%xc%cam)
513 if (hm%theory_level == rdmft) hm%exxop%useACE = .false.
514 else
515 call exchange_operator_init(hm%exxop, namespace, space, st, gr%der, mc, gr%stencil, &
516 hm%kpoints, cam_exact_exchange)
517 end if
518 end if
519
520 if (hm%is_applied_packed .and. accel_is_enabled()) then
521 ! Check if we can actually apply the hamiltonian packed
522 if (gr%use_curvilinear) then
523 if (accel_allow_cpu_only()) then
524 hm%is_applied_packed = .false.
525 call messages_write('Cannot use GPUs as curvilinear coordinates are used.')
526 call messages_warning(namespace=namespace)
527 else
528 call messages_write('Cannot use GPUs as curvilinear coordinates are used.', new_line = .true.)
529 call messages_write('Calculation will not be continued. To force execution, set AllowCPUonly = yes.')
530 call messages_fatal(namespace=namespace)
531 end if
532 end if
533 end if
534
535 !We are building the list of external potentials
536 !This is done here at the moment, because we pass directly the mesh
537 !TODO: Once the abstract Hamiltonian knows about an abstract basis, we might move this to the
538 ! abstract Hamiltonian
539 call load_external_potentials(hm%external_potentials, namespace)
540
541 !Some checks which are electron specific, like k-points
543
544 !At the moment we do only have static external potential, so we never update them
546
547 !Build the resulting interactions
548 !TODO: This will be moved to the actual interactions
549 call build_interactions()
550
551 ! Constrained DFT for noncollinear magnetism
552 if (hm%theory_level /= independent_particles) then
553 call magnetic_constrain_init(hm%magnetic_constrain, namespace, gr, st%d, ions%natoms, ions%min_distance())
554 end if
555
556 ! init maxwell-electrons coupling
557 call mxll_coupling_init(hm%mxll, st%d, gr, namespace, hm%mass)
558
559 if (associated(hm%xc_photons)) then
560 if (hm%xc_photons%wants_to_renormalize_mass()) then
561 ! remornalize the electron mass due to light-matter interaction; here we only deal with it in free space
562 call hm%set_mass(namespace, hm%xc_photons%get_renormalized_mass())
563 end if
564 end if
565
566 if (hm%xc%compute_exchange(hm%theory_level) .or. need_exchange_) call hm%exxop%write_info(namespace)
567
568 call profiling_out('HAMILTONIAN_ELEC_INIT')
569 pop_sub(hamiltonian_elec_init)
570
571 contains
572
573 ! ---------------------------------------------------------
574 subroutine build_external_potentials()
575 type(list_iterator_t) :: iter
576 class(*), pointer :: potential
577 integer :: iop
578
580
581 safe_allocate(hm%v_ext_pot(1:gr%np))
582 hm%v_ext_pot(1:gr%np) = m_zero
583
584 call iter%start(hm%external_potentials)
585 do while (iter%has_next())
586 potential => iter%get_next()
587 select type (potential)
588 class is (external_potential_t)
589
590 call potential%allocate_memory(gr)
591 call potential%calculate(namespace, gr, hm%psolver)
592 !To preserve the old behavior, we are adding the various potentials
593 !to the corresponding arrays
594 select case (potential%type)
596 call lalg_axpy(gr%np, m_one, potential%pot, hm%v_ext_pot)
597
599 if (states_are_real(st)) then
600 message(1) = "Cannot use static magnetic field with real wavefunctions"
601 call messages_fatal(1, namespace=namespace)
602 end if
603
604 if (.not. allocated(hm%ep%b_field)) then
605 safe_allocate(hm%ep%b_field(1:3)) !Cannot be space%dim
606 hm%ep%b_field(1:3) = m_zero
607 end if
608 hm%ep%b_field(1:3) = hm%ep%b_field(1:3) + potential%b_field(1:3)
609
610 if (.not. allocated(hm%ep%a_static)) then
611 safe_allocate(hm%ep%a_static(1:gr%np, 1:space%dim))
612 hm%ep%a_static(1:gr%np, 1:space%dim) = m_zero
613 end if
614 call lalg_axpy(gr%np, space%dim, m_one, potential%a_static, hm%ep%a_static)
615
617 if (.not. allocated(hm%ep%e_field)) then
618 safe_allocate(hm%ep%e_field(1:space%dim))
619 hm%ep%e_field(1:space%dim) = m_zero
620 end if
621 hm%ep%e_field(1:space%dim) = hm%ep%e_field(1:space%dim) + potential%e_field(1:space%dim)
622
623 !In the fully periodic case, we use Berry phases
624 if (space%periodic_dim < space%dim) then
625 if (.not. allocated(hm%v_static)) then
626 safe_allocate(hm%v_static(1:gr%np))
627 hm%v_static(1:gr%np) = m_zero
628 end if
629 if (.not. allocated(hm%ep%v_ext)) then
630 safe_allocate(hm%ep%v_ext(1:gr%np_part))
631 hm%ep%v_ext(1:gr%np_part) = m_zero
632 end if
633 call lalg_axpy(gr%np, m_one, potential%pot, hm%v_static)
634 call lalg_axpy(gr%np, m_one, potential%v_ext, hm%ep%v_ext)
635 end if
636
637 if (hm%kpoints%use_symmetries) then
638 do iop = 1, symmetries_number(hm%kpoints%symm)
639 if (iop == symmetries_identity_index(hm%kpoints%symm)) cycle
640 if (.not. symm_op_invariant_cart(hm%kpoints%symm%ops(iop), hm%ep%e_field, 1e-5_real64)) then
641 message(1) = "The StaticElectricField breaks (at least) one of the symmetries used to reduce the k-points."
642 message(2) = "Set SymmetryBreakDir equal to StaticElectricField."
643 call messages_fatal(2, namespace=namespace)
644 end if
645 end do
646 end if
647
648 end select
649 call potential%deallocate_memory()
650
651 class default
652 assert(.false.)
653 end select
654 end do
655
657 end subroutine build_external_potentials
658
659 ! ---------------------------------------------------------
660 subroutine external_potentials_checks()
661 type(list_iterator_t) :: iter
662 class(*), pointer :: potential
663
665
666 call iter%start(hm%external_potentials)
667 do while (iter%has_next())
668 potential => iter%get_next()
669 select type (potential)
670 class is (external_potential_t)
671
672 if (potential%type == external_pot_static_efield .and. hm%kpoints%reduced%npoints > 1) then
673 message(1) = "Applying StaticElectricField in a periodic direction is only accurate for large supercells."
674 message(2) = "Single-point Berry phase is not appropriate when k-point sampling is needed."
675 call messages_warning(2, namespace=namespace)
676 end if
677
678 class default
679 assert(.false.)
680 end select
681 end do
682
684 end subroutine external_potentials_checks
685
686
687 !The code in this routines needs to know about the external potentials.
688 !This will be treated in the future by the interactions directly.
689 subroutine build_interactions()
690 logical :: external_potentials_present
691 logical :: kick_present
692
694
695 if (allocated(hm%ep%e_field) .and. space%is_periodic() .and. .not. list_has_gauge_field(ext_partners)) then
696 ! only need vberry if there is a field in a periodic direction
697 ! and we are not setting a gauge field
698 if (any(abs(hm%ep%e_field(1:space%periodic_dim)) > m_epsilon)) then
699 safe_allocate(hm%vberry(1:gr%np, 1:hm%d%nspin))
700 hm%vberry = m_zero
701 end if
702 end if
703
704 external_potentials_present = epot_have_external_potentials(hm%ep) .or. &
705 list_has_lasers(ext_partners) .or. allocated(hm%v_static)
706
707
708 kick_present = hamiltonian_elec_has_kick(hm)
709
710 call pcm_init(hm%pcm, namespace, space, ions, gr, st%qtot, st%val_charge, external_potentials_present, kick_present)
711 if (hm%pcm%run_pcm) then
712 if (hm%theory_level /= kohn_sham_dft) call messages_not_implemented("PCM for TheoryLevel /= kohn_sham", namespace=namespace)
713 end if
714
716
717 end subroutine build_interactions
718
719
720 end subroutine hamiltonian_elec_init
721
722
723
724
725 ! ---------------------------------------------------------
726 subroutine hamiltonian_elec_end(hm)
727 type(hamiltonian_elec_t), target, intent(inout) :: hm
728
729 type(partner_iterator_t) :: iter
730 class(interaction_partner_t), pointer :: potential
731
733
734 if (hm%is_copy_snapshot) then
735 ! release only pointer targets created by hamiltonian_elec_copy
737 if (allocated(hm%lda_u)) then
738 safe_deallocate_a(hm%lda_u%basis)
739 end if
740 safe_deallocate_a(hm%lda_u)
741 safe_deallocate_a(hm%exxop)
742 safe_deallocate_a(hm%psolver)
743 safe_deallocate_a(hm%scissor)
744 safe_deallocate_a(hm%inh_st)
745 safe_deallocate_a(hm%ep)
746 safe_deallocate_a(hm%pcm)
747 pop_sub(hamiltonian_elec_end)
748 return
749 end if
750
751 call hm%hm_base%end()
752 call hm%vnl%end()
753 call hm%aug_overlap%end()
754 call hm%aug_inverse_overlap%end()
755
756 call hm%phase%end()
757
758 call hm%ks_pot%end()
759 safe_deallocate_a(hm%vberry)
760 safe_deallocate_a(hm%a_ind)
761 safe_deallocate_a(hm%b_ind)
762 safe_deallocate_a(hm%v_ext_pot)
763
764 safe_deallocate_p(hm%zora)
765
766 call poisson_end(hm%psolver)
767
768 nullify(hm%xc)
769
770 call kick_end(hm%kick)
771 call epot_end(hm%ep)
772 if (hm%owns_ions .and. associated(hm%ions)) then
773 ! ions_copy currently aliases species objects through species wrappers.
774 ! Detach wrapper storage to avoid double-finalizing shared species.
775 if (allocated(hm%ions%species)) then
776 safe_deallocate_a(hm%ions%species)
777 hm%ions%nspecies = 0
778 end if
779 safe_deallocate_p(hm%ions)
780 hm%owns_ions = .false.
781 else
782 nullify(hm%ions)
783 hm%owns_ions = .false.
784 end if
785
786 call absorbing_boundaries_end(hm%abs_boundaries)
787
788 call states_elec_dim_end(hm%d)
789
790 if (hm%scissor%apply) call scissor_end(hm%scissor)
791
792 call exchange_operator_end(hm%exxop)
793 call lda_u_end(hm%lda_u)
794
795 safe_deallocate_a(hm%energy)
796
797 if (hm%pcm%run_pcm) call pcm_end(hm%pcm)
799 call hm%v_ie_loc%end()
800 call hm%nlcc%end()
801
802 call iter%start(hm%external_potentials)
803 do while (iter%has_next())
804 potential => iter%get_next()
805 safe_deallocate_p(potential)
806 end do
807 call hm%external_potentials%empty()
808 safe_deallocate_a(hm%v_static)
809
810 call magnetic_constrain_end(hm%magnetic_constrain)
811
812 call mxll_coupling_end(hm%mxll)
813
814 hm%is_copy_snapshot = .false.
815
816 if (allocated(hm%lda_u)) then
817 safe_deallocate_a(hm%lda_u%basis)
818 end if
819 safe_deallocate_a(hm%lda_u)
820 safe_deallocate_a(hm%exxop)
821 safe_deallocate_a(hm%psolver)
822 safe_deallocate_a(hm%scissor)
823 safe_deallocate_a(hm%inh_st)
824 safe_deallocate_a(hm%ep)
825 safe_deallocate_a(hm%pcm)
826
827 pop_sub(hamiltonian_elec_end)
828 end subroutine hamiltonian_elec_end
829
830 ! ---------------------------------------------------------
835 type(hamiltonian_elec_t), target, intent(inout) :: hm
836
837 type(partner_iterator_t) :: iter
838 class(interaction_partner_t), pointer :: potential
839
841
842 if (hm%owns_ions .and. associated(hm%ions)) then
843 if (allocated(hm%ions%species)) then
844 safe_deallocate_a(hm%ions%species)
845 hm%ions%nspecies = 0
846 end if
847 safe_deallocate_p(hm%ions)
848 hm%owns_ions = .false.
849 else
850 nullify(hm%ions)
851 hm%owns_ions = .false.
852 end if
853
854 if (associated(hm%zora)) then
855 safe_deallocate_p(hm%zora)
856 end if
857
858 call iter%start(hm%external_potentials)
859 do while (iter%has_next())
860 potential => iter%get_next()
861 safe_deallocate_p(potential)
862 end do
863 call hm%external_potentials%empty()
864
865 call hm%hm_base%end()
866 call hm%ks_pot%end()
867 call hm%phase%end()
868 call hm%vnl%end()
869 call hm%aug_overlap%end()
870 call hm%aug_inverse_overlap%end()
871 if (hm%lda_u_level /= dft_u_none) call lda_u_end(hm%lda_u)
872
873 hm%is_copy_snapshot = .false.
874
877
878 ! ---------------------------------------------------------
885 subroutine hamiltonian_elec_copy(hm_out, hm_in)
886 type(hamiltonian_elec_t), target, intent(out) :: hm_out
887 type(hamiltonian_elec_t), intent(inout) :: hm_in
888
889 type(ions_t), pointer :: ions_out
890 type(zora_t), pointer :: zora_out
891 type(partner_list_t) :: external_potentials_in
892 type(partner_list_t) :: external_potentials_out
893 type(list_iterator_t) :: iter
894 class(*), pointer :: ptr
895 class(external_potential_t), pointer :: ext_pot
896 logical :: runtime_initialized
897
898 push_sub(hamiltonian_elec_copy)
899
901 runtime_initialized = associated(hm_in%kpoints)
902
903 call iter%start(hm_in%external_potentials)
904 do while (iter%has_next())
905 ptr => iter%get_next()
906 select type (ptr)
907 class is (external_potential_t)
908 call external_potentials_in%add(ptr)
909 call external_potential_clone(ext_pot, ptr)
910 call external_potentials_out%add(ext_pot)
911 class default
912 call messages_not_implemented("hamiltonian_elec_copy with unsupported external potential partner")
913 end select
914 end do
915 call hm_in%external_potentials%empty()
916
917 hm_out = hm_in
918 hm_out%is_copy_snapshot = .true.
919
920 call epot_bind_poisson_solver(hm_out%ep, hm_out%psolver)
921 call nonlocal_pseudopotential_rebind_projectors(hm_out%vnl, hm_out%ep)
922 call nonlocal_pseudopotential_rebind_projectors(hm_out%aug_overlap, hm_out%ep)
923 call nonlocal_pseudopotential_rebind_projectors(hm_out%aug_inverse_overlap, hm_out%ep)
924
925 if (accel_is_enabled() .and. runtime_initialized .and. associated(hm_out%der)) then
926 call hamiltonian_elec_base_accel_rebuild(hm_out%hm_base, hm_out%der%mesh)
927 call ks_potential_accel_rebuild(hm_out%ks_pot)
928 call phase_accel_rebuild(hm_out%phase, hm_out%der%mesh, hm_out%d%kpt)
929 call nonlocal_pseudopotential_accel_rebuild(hm_out%vnl, hm_out%space, hm_out%der%mesh)
930 call nonlocal_pseudopotential_accel_rebuild(hm_out%aug_overlap, hm_out%space, hm_out%der%mesh)
931 call nonlocal_pseudopotential_accel_rebuild(hm_out%aug_inverse_overlap, hm_out%space, hm_out%der%mesh)
932 end if
933
934 call hm_out%external_potentials%empty()
935 call iter%start(external_potentials_out)
936 do while (iter%has_next())
937 ptr => iter%get_next()
938 select type (ptr)
939 class is (external_potential_t)
940 call hm_out%external_potentials%add(ptr)
941 class default
942 call messages_not_implemented("hamiltonian_elec_copy with unsupported external potential partner")
943 end select
944 end do
945 call iter%start(external_potentials_in)
946 do while (iter%has_next())
947 ptr => iter%get_next()
948 select type (ptr)
949 class is (external_potential_t)
950 call hm_in%external_potentials%add(ptr)
951 class default
952 call messages_not_implemented("hamiltonian_elec_copy with unsupported external potential partner")
953 end select
954 end do
955 call external_potentials_out%empty()
956 call external_potentials_in%empty()
958 if (runtime_initialized) then
959 call kick_end(hm_out%kick)
960 call kick_copy(hm_out%kick, hm_in%kick)
961
962 if (allocated(hm_in%energy)) then
963 if (.not. allocated(hm_out%energy)) safe_allocate(hm_out%energy)
964 call energy_copy(hm_in%energy, hm_out%energy)
965 else
966 safe_deallocate_a(hm_out%energy)
967 end if
968
969 call magnetic_constrain_copy(hm_out%magnetic_constrain, hm_in%magnetic_constrain)
970 call mxll_coupling_copy(hm_out%mxll, hm_in%mxll, hm_out%der)
971 end if
972
973 if (associated(hm_in%ions)) then
974 safe_allocate(ions_out)
975 ions_out = hm_in%ions
976 hm_out%ions => ions_out
977 hm_out%owns_ions = .true.
978 call hm_out%v_ie_loc%bind(hm_out%psolver, hm_out%ions)
979 call hm_out%nlcc%bind(hm_out%ions)
980 else
981 nullify(hm_out%ions)
982 hm_out%owns_ions = .false.
983 nullify(hm_out%v_ie_loc%atoms_dist)
984 nullify(hm_out%v_ie_loc%atom)
985 nullify(hm_out%v_ie_loc%pos)
986 nullify(hm_out%nlcc%atoms_dist)
987 nullify(hm_out%nlcc%atom)
988 nullify(hm_out%nlcc%pos)
989 end if
990
991 if (hm_out%lda_u_level /= dft_u_none .and. associated(hm_out%ions)) then
992 call lda_u_rebind_after_copy(hm_out%lda_u, hm_out%ions)
993 if (accel_is_enabled() .and. runtime_initialized) then
994 call lda_u_accel_rebuild(hm_out%lda_u, hm_out%d%kpt)
995 end if
996 end if
997
998 if (associated(hm_in%zora)) then
999 allocate(zora_out, source=hm_in%zora)
1000 hm_out%zora => zora_out
1001 else
1002 nullify(hm_out%zora)
1003 end if
1004
1005 pop_sub(hamiltonian_elec_copy)
1006
1007 end subroutine hamiltonian_elec_copy
1008
1009 ! ---------------------------------------------------------
1012 type(hamiltonian_elec_t), intent(in) :: hm_in
1013
1014 logical :: runtime_initialized
1015
1017
1018 runtime_initialized = associated(hm_in%kpoints)
1019 if (.not. runtime_initialized) then
1021 return
1022 end if
1023
1024 if (hm_in%theory_level == hartree_fock .or. hm_in%theory_level == rdmft) then
1025 call messages_not_implemented("hamiltonian_elec_copy with HF/RDMFT exchange operator paths")
1026 end if
1027
1028 if (associated(hm_in%xc)) then
1029 if (hm_in%xc%compute_exchange(hm_in%theory_level)) then
1030 call messages_not_implemented("hamiltonian_elec_copy with hybrid/OEP exchange paths")
1031 end if
1032 end if
1033
1034 if (hamiltonian_elec_inh_term(hm_in)) then
1035 call messages_not_implemented("hamiltonian_elec_copy with OCT inhomogeneous/source term")
1036 end if
1037
1038 if (oct_exchange_enabled(hm_in%oct_exchange)) then
1039 call messages_not_implemented("hamiltonian_elec_copy with OCT exchange term")
1040 end if
1041
1042 if (hm_in%pcm%run_pcm) then
1043 call messages_not_implemented("hamiltonian_elec_copy with PCM enabled")
1044 end if
1045
1048
1049
1050 ! ---------------------------------------------------------
1051 ! True if the Hamiltonian is Hermitian, false otherwise
1052 logical function hamiltonian_elec_hermitian(hm)
1053 class(hamiltonian_elec_t), intent(in) :: hm
1054
1056 hamiltonian_elec_hermitian = .not.((hm%abs_boundaries%abtype == imaginary_absorbing) .or. &
1057 oct_exchange_enabled(hm%oct_exchange))
1058
1060 end function hamiltonian_elec_hermitian
1061
1062 ! ---------------------------------------------------------
1063 ! True if the Hamiltonian needs to apply the mGGA term
1064 pure logical function hamiltonian_elec_needs_mgga_term(hm, terms)
1065 class(hamiltonian_elec_t), intent(in) :: hm
1066 integer, intent(in) :: terms
1067
1069
1070 if (bitand(term_mgga, terms) /= 0 .and. family_is_mgga_with_exc(hm%xc) &
1071 .and. hm%theory_level == generalized_kohn_sham_dft) then
1073 end if
1074
1075 !For OEP
1076 if(terms == term_mgga .and. family_is_mgga_with_exc(hm%xc) .and. hm%theory_level == kohn_sham_dft) then
1078 end if
1079
1081
1082
1084 ! ---------------------------------------------------------
1085 subroutine hamiltonian_elec_span(hm, delta, emin, namespace)
1086 class(hamiltonian_elec_t), intent(inout) :: hm
1087 real(real64), intent(in) :: delta(:)
1088 real(real64), intent(in) :: emin
1089 type(namespace_t), intent(in) :: namespace
1090
1091 real(real64) :: emax
1092
1093 push_sub(hamiltonian_elec_span)
1094
1095 ! estimate maximum energy of discrete kinetic operator
1096 ! this neglects possible contributions from the non-local part of the pseudopotentials
1098
1099 hm%spectral_middle_point = (emax + emin) / m_two
1100 hm%spectral_half_span = (emax - emin) / m_two
1101
1102 pop_sub(hamiltonian_elec_span)
1103 end subroutine hamiltonian_elec_span
1104
1105
1106 ! ---------------------------------------------------------
1107 pure logical function hamiltonian_elec_inh_term(hm) result(inh)
1108 type(hamiltonian_elec_t), intent(in) :: hm
1109
1110 inh = hm%inh_term
1111 end function hamiltonian_elec_inh_term
1112
1113
1114 ! ---------------------------------------------------------
1115 subroutine hamiltonian_elec_set_inh(hm, st)
1116 type(hamiltonian_elec_t), intent(inout) :: hm
1117 type(states_elec_t), intent(in) :: st
1118
1119 push_sub(hamiltonian_elec_set_inh)
1120
1121 if (hm%inh_term) call states_elec_end(hm%inh_st)
1122 call states_elec_copy(hm%inh_st, st)
1123 hm%inh_term = .true.
1126 end subroutine hamiltonian_elec_set_inh
1127
1128
1129 ! ---------------------------------------------------------
1130 subroutine hamiltonian_elec_remove_inh(hm)
1131 type(hamiltonian_elec_t), intent(inout) :: hm
1132
1134
1135 if (hm%inh_term) then
1136 call states_elec_end(hm%inh_st)
1137 hm%inh_term = .false.
1138 end if
1139
1141 end subroutine hamiltonian_elec_remove_inh
1142
1143 ! ---------------------------------------------------------
1144 subroutine hamiltonian_elec_adjoint(hm)
1145 type(hamiltonian_elec_t), intent(inout) :: hm
1146
1147 push_sub(hamiltonian_elec_adjoint)
1148
1149 if (.not. hm%adjoint) then
1150 hm%adjoint = .true.
1151 if (hm%abs_boundaries%abtype == imaginary_absorbing) then
1152 hm%abs_boundaries%mf = -hm%abs_boundaries%mf
1153 end if
1154 end if
1155
1158
1159
1160 ! ---------------------------------------------------------
1161 subroutine hamiltonian_elec_not_adjoint(hm)
1162 type(hamiltonian_elec_t), intent(inout) :: hm
1163
1165
1166 if (hm%adjoint) then
1167 hm%adjoint = .false.
1168 if (hm%abs_boundaries%abtype == imaginary_absorbing) then
1169 hm%abs_boundaries%mf = -hm%abs_boundaries%mf
1170 end if
1171 end if
1172
1174 end subroutine hamiltonian_elec_not_adjoint
1175
1176
1177 ! ---------------------------------------------------------
1179 subroutine hamiltonian_elec_update(this, mesh, namespace, space, ext_partners, time)
1180 class(hamiltonian_elec_t), intent(inout) :: this
1181 class(mesh_t), intent(in) :: mesh
1182 type(namespace_t), intent(in) :: namespace
1183 class(space_t), intent(in) :: space
1184 type(partner_list_t), intent(in) :: ext_partners
1185 real(real64), optional, intent(in) :: time
1186
1187 integer :: ispin, ip, idir, iatom, ilaser
1188 real(real64) :: aa(space%dim), time_
1189 real(real64), allocatable :: vp(:,:)
1190 type(lasers_t), pointer :: lasers
1191 type(gauge_field_t), pointer :: gfield
1192 real(real64) :: am(space%dim)
1193
1194 push_sub(hamiltonian_elec_update)
1195 call profiling_in("HAMILTONIAN_ELEC_UPDATE")
1196
1197 this%current_time = m_zero
1198 if (present(time)) this%current_time = time
1199
1200 time_ = optional_default(time, 0.0_real64)
1201
1202 ! set everything to zero
1203 call this%hm_base%clear(mesh%np)
1204
1205 ! alllocate the scalar potential for the xc, hartree and external potentials
1206 call this%hm_base%allocate_field(mesh, field_potential, &
1207 complex_potential = this%abs_boundaries%abtype == imaginary_absorbing)
1208
1209 ! the lasers
1210 if (present(time) .or. this%time_zero) then
1211
1212 lasers => list_get_lasers(ext_partners)
1213 if(associated(lasers)) then
1214 do ilaser = 1, lasers%no_lasers
1215 select case (laser_kind(lasers%lasers(ilaser)))
1217 do ispin = 1, this%d%spin_channels
1218 call laser_potential(lasers%lasers(ilaser), mesh, &
1219 this%hm_base%potential(:, ispin), time_)
1220 end do
1221 case (e_field_magnetic)
1222 call this%hm_base%allocate_field(mesh, field_vector_potential + field_uniform_magnetic_field, &
1223 .false.)
1224 ! get the vector potential
1225 safe_allocate(vp(1:mesh%np, 1:space%dim))
1226 vp(1:mesh%np, 1:space%dim) = m_zero
1227 call laser_vector_potential(lasers%lasers(ilaser), mesh, vp, time_)
1228 !$omp parallel do private(idir) schedule(static)
1229 do ip = 1, mesh%np
1230 do idir = 1, space%dim
1231 this%hm_base%vector_potential(idir, ip) = this%hm_base%vector_potential(idir, ip) + vp(ip, idir)/p_c
1232 end do
1233 end do
1234 ! and the magnetic field
1235 call laser_field(lasers%lasers(ilaser), this%hm_base%uniform_magnetic_field(1:space%dim), time_)
1236 safe_deallocate_a(vp)
1238 call this%hm_base%allocate_field(mesh, field_uniform_vector_potential, .false.)
1239 ! get the uniform vector potential associated with a magnetic field
1240 aa = m_zero
1241 call laser_field(lasers%lasers(ilaser), aa, time_)
1242 this%hm_base%uniform_vector_potential(1:space%dim) = this%hm_base%uniform_vector_potential(1:space%dim) - aa/p_c
1243 end select
1244 end do
1245
1246 if (lasers_with_nondipole_field(lasers)) then
1247 assert( allocated(this%hm_base%uniform_vector_potential))
1248 call lasers_nondipole_laser_field_step(lasers, am, time_)
1249 this%hm_base%uniform_vector_potential(1:space%dim) = this%hm_base%uniform_vector_potential(1:space%dim) - am/p_c
1250 end if
1251 end if
1252
1253 ! the gauge field
1254 gfield => list_get_gauge_field(ext_partners)
1255 if (associated(gfield)) then
1256 call this%hm_base%allocate_field(mesh, field_uniform_vector_potential, .false.)
1257 call gauge_field_get_vec_pot(gfield, aa)
1258 this%hm_base%uniform_vector_potential(1:space%dim) = this%hm_base%uniform_vector_potential(1:space%dim) - aa/p_c
1259 end if
1260
1261 ! the electric field for a periodic system through the gauge field
1262 if (allocated(this%ep%e_field) .and. associated(gfield)) then
1263 this%hm_base%uniform_vector_potential(1:space%periodic_dim) = &
1264 this%hm_base%uniform_vector_potential(1:space%periodic_dim) - time_*this%ep%e_field(1:space%periodic_dim)
1265 end if
1266
1267 ! add the photon-free mean-field vector potential
1268 if (associated(this%xc_photons)) then
1269 if(this%xc_photons%lpfmf .and. allocated(this%xc_photons%mf_vector_potential)) then
1270 call this%hm_base%allocate_field(mesh, field_uniform_vector_potential, .false.)
1271 ! here we put a minus sign in front of the mean field term to get the right answer (need to check the formula)
1272 this%hm_base%uniform_vector_potential(1:space%dim) = &
1273 this%hm_base%uniform_vector_potential(1:space%dim) - this%xc_photons%mf_vector_potential(1:space%dim)/p_c
1274 end if
1275 end if
1276
1277 end if
1278
1279 ! the vector potential of a static magnetic field
1280 if (allocated(this%ep%a_static)) then
1281 call this%hm_base%allocate_field(mesh, field_vector_potential, .false.)
1282 !ep%a_static contains 1/c A(r)
1283 !$omp parallel do private(idir) schedule(static)
1284 do ip = 1, mesh%np
1285 do idir = 1, space%dim
1286 this%hm_base%vector_potential(idir, ip) = this%hm_base%vector_potential(idir, ip) + this%ep%a_static(ip, idir)
1287 end do
1288 end do
1289 end if
1290
1291 ! add Maxwell coupling to Hamiltonian and sets the magnetic field for the Zeeman term added below
1292 call mxll_coupling_calc(this%mxll, this%hm_base, mesh, this%d, space)
1293
1294 !The electric field was added to the KS potential
1295 call this%hm_base%accel_copy_pot(mesh)
1296
1297 ! and the static magnetic field
1298 if (allocated(this%ep%b_field)) then
1299 call this%hm_base%allocate_field(mesh, field_uniform_magnetic_field, .false.)
1300 do idir = 1, 3
1301 this%hm_base%uniform_magnetic_field(idir) = this%hm_base%uniform_magnetic_field(idir) + this%ep%b_field(idir)
1302 end do
1303 end if
1304
1305 ! Combine the uniform and non-uniform fields and compute the Zeeman term
1306 call this%hm_base%update_magnetic_terms(mesh, this%ep%gyromagnetic_ratio, this%d%ispin)
1307
1308 ! This needs to be called at the end as the zeeman term enters the potential
1309 call hamiltonian_elec_update_pot(this, mesh, accumulate = .true.)
1310
1311 if (this%mxll%test_equad) then
1312 call set_electric_quadrupole_pot(this%mxll, mesh)
1313 end if
1314
1315 call build_phase()
1316
1317 ! Note: the ultrasoft/PAW screening D_ij = D_ij^(0) + int V_s(r) Q_ij(r) dr is done
1318 ! in hamiltonian_elec_update_pot (called above via hamiltonian_elec_update_pot with
1319 ! accumulate=.true.), where the full local potential V_s = V_loc + V_Hxc is assembled.
1320
1321 call profiling_out("HAMILTONIAN_ELEC_UPDATE")
1323
1324 contains
1325
1326 subroutine build_phase()
1327
1329
1330 if ((.not. this%kpoints%gamma_only()) .or. allocated(this%hm_base%uniform_vector_potential)) then
1331
1332 call profiling_in('UPDATE_PHASES')
1333 ! now regenerate the phases for the pseudopotentials
1334 do iatom = 1, this%ep%natoms
1335 call projector_init_phases(this%ep%proj(iatom), space%dim, this%d, this%der%boundaries, this%kpoints, &
1336 vec_pot = this%hm_base%uniform_vector_potential, vec_pot_var = this%hm_base%vector_potential)
1337 end do
1338
1339 call profiling_out('UPDATE_PHASES')
1340 end if
1341
1342 if (allocated(this%hm_base%uniform_vector_potential)) then
1343
1344 call this%phase%update(mesh, this%d%kpt, this%kpoints, this%d, space, this%hm_base%uniform_vector_potential)
1345
1346 ! We rebuild the phase for the orbital projection, similarly to the one of the pseudopotentials
1347 if (this%lda_u_level /= dft_u_none) then
1348 call lda_u_build_phase_correction(this%lda_u, space, this%d, this%der%boundaries, namespace, this%kpoints, &
1349 vec_pot = this%hm_base%uniform_vector_potential, vec_pot_var = this%hm_base%vector_potential)
1350 end if
1351 end if
1352
1353 call hamiltonian_elec_build_projector_phases(this, this%vnl, async=.true.)
1354 if (this%ep%requires_generalized_overlap) then
1355 call hamiltonian_elec_build_projector_phases(this, this%aug_overlap, async=.true.)
1356 call hamiltonian_elec_build_projector_phases(this, this%aug_inverse_overlap, async=.true.)
1357 ! Exact inverse overlap
1358 if (this%aug_inverse_overlap%ovlp_inv%use_global_coupling) then
1359 if (.not. this%aug_inverse_overlap%ovlp_inv%coupling_built .or. this%phase%is_allocated()) then
1360 call this%aug_inverse_overlap%ovlp_inv%build(this%aug_inverse_overlap%projector_matrices, &
1361 this%aug_inverse_overlap%projector_phases, mesh, this%d%kpt%start, this%d%kpt%end)
1362 end if
1363 end if
1364 end if
1365
1366 call accel_finish()
1367
1369 end subroutine build_phase
1370
1371 end subroutine hamiltonian_elec_update
1372
1373
1374 !----------------------------------------------------------------
1377 ! TODO: See Issue #1064
1378 subroutine hamiltonian_elec_update_pot(this, mesh, accumulate)
1379 type(hamiltonian_elec_t), intent(inout) :: this
1380 class(mesh_t), intent(in) :: mesh
1381 logical, optional, intent(in) :: accumulate
1382
1383 integer :: ispin, ip
1384
1386
1387 ! By default we nullify first the result
1388 if (.not. optional_default(accumulate, .false.)) then
1389 !$omp parallel private(ip, ispin)
1390 do ispin = 1, this%d%nspin
1391 !$omp do simd schedule(static)
1392 do ip = 1, mesh%np
1393 this%hm_base%potential(ip, ispin) = m_zero
1394 end do
1395 end do
1396 !$omp end parallel
1397 if (this%abs_boundaries%abtype == imaginary_absorbing) then
1398 this%hm_base%Impotential = m_zero
1399 end if
1400 end if
1401
1402 !$omp parallel private(ip, ispin)
1403 do ispin = 1, this%d%nspin
1404 if (ispin <= 2) then
1405 !$omp do simd schedule(static)
1406 ! this%vhxc(ip, ispin) is added after the calculation of ZORA potential
1407 do ip = 1, mesh%np
1408 this%hm_base%potential(ip, ispin) = this%hm_base%potential(ip, ispin) + this%ep%vpsl(ip) + this%v_ext_pot(ip)
1409 end do
1410
1412 if (this%pcm%run_pcm) then
1413 if (this%pcm%solute) then
1414 !$omp do simd schedule(static)
1415 do ip = 1, mesh%np
1416 this%hm_base%potential(ip, ispin) = this%hm_base%potential(ip, ispin) + &
1417 this%pcm%v_e_rs(ip) + this%pcm%v_n_rs(ip)
1418 end do
1419 !$omp end do simd nowait
1420 end if
1421 if (this%pcm%localf) then
1422 !$omp do simd schedule(static)
1423 do ip = 1, mesh%np
1424 this%hm_base%potential(ip, ispin) = this%hm_base%potential(ip, ispin) + &
1425 this%pcm%v_ext_rs(ip)
1426 end do
1427 !$omp end do simd nowait
1428 end if
1429 end if
1430
1432 if (this%abs_boundaries%abtype == imaginary_absorbing) then
1433 !$omp do simd schedule(static)
1434 do ip = 1, mesh%np
1435 this%hm_base%Impotential(ip, ispin) = this%hm_base%Impotential(ip, ispin) + this%abs_boundaries%mf(ip)
1436 end do
1437 !$omp end do simd nowait
1438 end if
1439 end if
1440 end do
1441 !$omp end parallel
1442
1443 ! scalar relativistic ZORA contribution
1444 ! \boldsymbol{p} \frac{c^2}{2c^2 - V} \boldsymbol{p} \Phi^\mathrm{ZORA}
1445 ! Note that V here is the ionic plus external potential only: vHxc is added below, and is
1446 ! deliberately excluded from the ZORA prefactor. This makes it comparable to a one-electron
1447 ! X2C rather than to a full ZORA, and it is what allows zora_update to treat V as spin
1448 ! independent. Do not move this call after add_vhxc without revisiting zora_update.
1449 if (this%ep%reltype == scalar_relativistic_zora .or. this%ep%reltype == fully_relativistic_zora) then
1450 call this%zora%update(this%der, this%hm_base%potential)
1451 end if
1452
1453 !$omp parallel private(ip, ispin)
1454 do ispin = 1, this%d%nspin
1455 ! Adding Zeeman potential to hm_base%potential
1456 if (allocated(this%hm_base%zeeman_pot)) then
1457 !$omp do simd schedule(static)
1458 do ip = 1, mesh%np
1459 this%hm_base%potential(ip, ispin) = this%hm_base%potential(ip, ispin) + this%hm_base%zeeman_pot(ip, ispin)
1460 end do
1461 !$omp end do simd nowait
1462 end if
1463
1464 ! Adding Quadrupole potential from static E-field (test)
1465 if (this%mxll%test_equad) then
1466 !$omp do simd schedule(static)
1467 do ip = 1, mesh%np
1468 this%hm_base%potential(ip, ispin) = this%hm_base%potential(ip, ispin) + this%mxll%e_quadrupole_pot(ip)
1469 end do
1470 !$omp end do simd
1471 end if
1472 end do
1473 !$omp end parallel
1474
1475
1476 ! Add the Hartree and KS potential
1477 call this%ks_pot%add_vhxc(this%hm_base%potential)
1478
1479 call this%hm_base%accel_copy_pot(mesh)
1480
1481 ! For ultrasoft/PAW, screen the nonlocal coupling with the freshly assembled
1482 ! full local potential V_s = V_loc + V_Hxc: D_ij = D_ij^(0) + int V_s(r) Q_ij(r) dr.
1483 if (this%ep%requires_generalized_overlap) then
1484 ! For PAW the one-center density matrix (onsite_density_matrix) is accumulated during the density
1485 ! calculation on the overlap instance; pass it so update_dij can build the one-center
1486 ! Hartree/XC coupling (D^1 - D~^1)_Hxc and energy correction. Once the projectors are built
1487 ! the matrix must exist: without it the one-center terms cannot be evaluated.
1488 if (this%ep%has_paw .and. this%vnl%nprojector_matrices > 0) then
1489 if (.not. allocated(this%aug_overlap%onsite_density_matrix)) then
1490 message(1) = "Internal error: the PAW one-center density matrix is not allocated,"
1491 message(2) = "the one-center contributions to the Hamiltonian cannot be computed."
1492 call messages_fatal(2)
1493 end if
1494 call nonlocal_pseudopotential_update_dij(this%vnl, mesh, this%ep, this%hm_base%potential, &
1495 onsite_density_matrix = this%aug_overlap%onsite_density_matrix)
1496 else
1497 call nonlocal_pseudopotential_update_dij(this%vnl, mesh, this%ep, this%hm_base%potential)
1498 end if
1499 end if
1500
1502 end subroutine hamiltonian_elec_update_pot
1503
1504 ! ---------------------------------------------------------
1505 subroutine hamiltonian_elec_epot_generate(this, namespace, space, gr, ions, ext_partners, st, time)
1506 type(hamiltonian_elec_t), intent(inout) :: this
1507 type(namespace_t), intent(in) :: namespace
1508 class(electron_space_t), intent(in) :: space
1509 type(grid_t), intent(in) :: gr
1510 type(ions_t), target, intent(inout) :: ions
1511 type(partner_list_t), intent(in) :: ext_partners
1512 type(states_elec_t), intent(inout) :: st
1513 real(real64), optional, intent(in) :: time
1514
1516
1517 this%ions => ions
1518 call epot_generate(this%ep, namespace, gr, this%ions, this%d)
1519 st%requires_generalized_overlap = this%ep%requires_generalized_overlap
1520
1521 ! Interation terms are treated below
1522
1523 ! First we add the static electric field
1524 if (allocated(this%ep%e_field) .and. space%periodic_dim < space%dim) then
1525 call lalg_axpy(gr%np, m_one, this%v_static, this%ep%vpsl)
1526 end if
1527
1528 ! Here we need to pass this again, else test are failing.
1529 ! This is not a real problem, as the multisystem framework will indeed to this anyway
1530 this%v_ie_loc%atoms_dist => ions%atoms_dist
1531 this%v_ie_loc%atom => ions%atom
1532 call this%v_ie_loc%calculate()
1533
1534 ! At the moment we need to add this to ep%vpsl, to keep the behavior of the code
1535 call lalg_axpy(gr%np, m_one, this%v_ie_loc%potential(:,1), this%ep%vpsl)
1536
1537 ! Here we need to reinit the NLCC object
1538 ! This is not a real problem, as the multisystem framework will indeed to this anyway
1539 if (this%ep%nlcc) then
1540 call this%nlcc%end()
1541 call this%nlcc%init(gr, ions, this%ep%dgrid)
1542 call this%nlcc%calculate()
1543 call lalg_copy(gr%np, this%nlcc%density(:,1), st%rho_core)
1544 end if
1545
1546 call this%vnl%build(space, gr, this%ep, this%d%ispin)
1547 if (this%ep%requires_generalized_overlap) then
1548 call this%aug_overlap%build(space, gr, this%ep, this%d%ispin, overlap_operator = .true.)
1549 call this%aug_inverse_overlap%build(space, gr, this%ep, this%d%ispin, inverse_overlap_operator = .true.)
1550 ! PAW: precompute the crystal-symmetry data to symmetrize the one-center density matrix
1551 ! (accumulated on the overlap instance during the density calculation).
1552 if (this%ep%has_paw) then
1553 call nonlocal_pseudopotential_build_paw_symm(this%aug_overlap, this%ep, space, gr, ions)
1554 end if
1555 else
1556 call this%aug_overlap%end()
1557 call this%aug_inverse_overlap%end()
1558 end if
1559 call hamiltonian_elec_update(this, gr, namespace, space, ext_partners, time)
1560
1561 ! Check if projectors are still compatible with apply_packed on GPU
1562 if (this%is_applied_packed .and. accel_is_enabled()) then
1563 if (this%ep%non_local .and. .not. this%vnl%apply_projector_matrices) then
1564 if (accel_allow_cpu_only()) then
1565 call messages_write('Relativistic pseudopotentials have not been fully implemented for GPUs.')
1566 call messages_warning(namespace=namespace)
1567 else
1568 call messages_write('Relativistic pseudopotentials have not been fully implemented for GPUs.',&
1569 new_line = .true.)
1570 call messages_write('Calculation will not be continued. To force execution, set AllowCPUonly = yes.')
1571 call messages_fatal(namespace=namespace)
1572 end if
1573 end if
1574
1575 end if
1576
1577 if (this%pcm%run_pcm) then
1580 if (this%pcm%solute) then
1581 call pcm_calc_pot_rs(this%pcm, gr, this%psolver, ions = ions)
1582 end if
1583
1586 ! Interpolation is needed, hence gr%np_part -> 1:gr%np
1587 if (this%pcm%localf .and. allocated(this%v_static)) then
1588 call pcm_calc_pot_rs(this%pcm, gr, this%psolver, v_ext = this%ep%v_ext(1:gr%np_part))
1589 end if
1590
1591 end if
1592
1593 call lda_u_update_basis(this%lda_u, space, gr, ions, st, this%psolver, namespace, this%kpoints, &
1594 this%phase%is_allocated(), this%aug_overlap)
1595
1597 end subroutine hamiltonian_elec_epot_generate
1598
1599 ! -----------------------------------------------------------------
1600
1601 real(real64) function hamiltonian_elec_get_time(this) result(time)
1602 type(hamiltonian_elec_t), intent(inout) :: this
1603
1604 time = this%current_time
1605 end function hamiltonian_elec_get_time
1606
1607 ! -----------------------------------------------------------------
1608
1609 pure logical function hamiltonian_elec_apply_packed(this) result(apply)
1610 class(hamiltonian_elec_t), intent(in) :: this
1611
1612 apply = this%is_applied_packed
1613
1615
1616
1617 ! -----------------------------------------------------------------
1618 subroutine zhamiltonian_elec_apply_atom (hm, namespace, space, latt, species, pos, ia, mesh, psi, vpsi)
1619 type(hamiltonian_elec_t), intent(in) :: hm
1620 type(namespace_t), intent(in) :: namespace
1621 class(space_t), intent(in) :: space
1622 type(lattice_vectors_t), intent(in) :: latt
1623 class(species_t), intent(in) :: species
1624 real(real64), intent(in) :: pos(1:space%dim)
1625 integer, intent(in) :: ia
1626 class(mesh_t), intent(in) :: mesh
1627 complex(real64), contiguous, intent(in) :: psi(:,:)
1628 complex(real64), contiguous, intent(out) :: vpsi(:,:)
1629
1630 integer :: idim, ip
1631 real(real64), allocatable :: vlocal(:)
1633
1634 safe_allocate(vlocal(1:mesh%np_part))
1635 vlocal = m_zero
1636 call epot_local_potential(hm%ep, namespace, space, latt, mesh, species, pos, ia, vlocal)
1637
1638 !$omp parallel private(idim, ip)
1639 do idim = 1, hm%d%dim
1640 !$omp do
1641 do ip = 1, mesh%np
1642 vpsi(ip, idim) = vlocal(ip) * psi(ip, idim)
1643 end do
1644 end do
1645 !$omp end parallel
1646
1647 safe_deallocate_a(vlocal)
1649 end subroutine zhamiltonian_elec_apply_atom
1650
1651 ! ---------------------------------------------------------
1656 subroutine hamiltonian_elec_update_with_ext_pot(this, mesh, space, ext_partners, time, mu)
1657 type(hamiltonian_elec_t), intent(inout) :: this
1658 class(space_t), intent(in) :: space
1659 class(mesh_t), intent(in) :: mesh
1660 type(partner_list_t), intent(in) :: ext_partners
1661 real(real64), intent(in) :: time(1:2)
1662 real(real64), intent(in) :: mu(1:2)
1663
1664 integer :: ispin, ip, idir, iatom, ilaser, itime
1665 real(real64) :: aa(space%dim), bb(space%dim), time_
1666 real(real64), allocatable :: vp(:,:)
1667 real(real64), allocatable :: velectric(:)
1668 type(lasers_t), pointer :: lasers
1669 type(gauge_field_t), pointer :: gfield
1670
1672 call profiling_in("HAMILTONIAN_ELEC_UPDATE_EXT_POT")
1674 this%current_time = m_zero
1675 this%current_time = time(1)
1676
1677 ! set everything to zero
1678 call this%hm_base%clear(mesh%np)
1679
1680 ! the xc, hartree and external potentials
1681 call this%hm_base%allocate_field(mesh, field_potential, &
1682 complex_potential = this%abs_boundaries%abtype == imaginary_absorbing)
1683
1684 do itime = 1, 2
1685 time_ = time(itime)
1686
1687 lasers => list_get_lasers(ext_partners)
1688 if(associated(lasers)) then
1689 do ilaser = 1, lasers%no_lasers
1690 select case (laser_kind(lasers%lasers(ilaser)))
1691 case (e_field_scalar_potential, e_field_electric)
1692 safe_allocate(velectric(1:mesh%np))
1693 do ispin = 1, this%d%spin_channels
1694 velectric = m_zero
1695 call laser_potential(lasers%lasers(ilaser), mesh, velectric, time_)
1696 !$omp parallel do simd schedule(static)
1697 do ip = 1, mesh%np
1698 this%hm_base%potential(ip, ispin) = this%hm_base%potential(ip, ispin) + mu(itime) * velectric(ip)
1699 end do
1700 end do
1701 safe_deallocate_a(velectric)
1702 case (e_field_magnetic)
1703 call this%hm_base%allocate_field(mesh, field_vector_potential + field_uniform_magnetic_field, .false.)
1704 ! get the vector potential
1705 safe_allocate(vp(1:mesh%np, 1:space%dim))
1706 vp(1:mesh%np, 1:space%dim) = m_zero
1707 call laser_vector_potential(lasers%lasers(ilaser), mesh, vp, time_)
1708 do idir = 1, space%dim
1709 !$omp parallel do schedule(static)
1710 do ip = 1, mesh%np
1711 this%hm_base%vector_potential(idir, ip) = this%hm_base%vector_potential(idir, ip) &
1712 + mu(itime) * vp(ip, idir)/p_c
1713 end do
1714 end do
1715 ! and the magnetic field
1716 bb = m_zero
1717 call laser_field(lasers%lasers(ilaser), bb(1:space%dim), time_)
1718 this%hm_base%uniform_magnetic_field = this%hm_base%uniform_magnetic_field &
1719 + mu(itime) * bb
1720 safe_deallocate_a(vp)
1721 case (e_field_vector_potential)
1722 call this%hm_base%allocate_field(mesh, field_uniform_vector_potential, .false.)
1723 ! get the uniform vector potential associated with a magnetic field
1724 aa = m_zero
1725 call laser_field(lasers%lasers(ilaser), aa, time_)
1726 this%hm_base%uniform_vector_potential(1:space%dim) = this%hm_base%uniform_vector_potential(1:space%dim) &
1727 - mu(itime) * aa/p_c
1728 end select
1729 end do
1730 end if
1731
1732 ! the gauge field
1733 gfield => list_get_gauge_field(ext_partners)
1734 if (associated(gfield)) then
1735 call this%hm_base%allocate_field(mesh, field_uniform_vector_potential, .false.)
1736 call gauge_field_get_vec_pot(gfield, aa)
1737 this%hm_base%uniform_vector_potential(1:space%dim) = this%hm_base%uniform_vector_potential(1:space%dim) - aa/p_c
1738 end if
1739
1740 ! the electric field for a periodic system through the gauge field
1741 ! TODO: The condition is wrong here: the e_field should be in non-periodic dims as E field
1742 ! and as a gauge field in the periodic dim, unless we use a Bery phase, in which, we do not use it
1743 ! this way. But this is unrelated to the gauge field
1744 if (allocated(this%ep%e_field) .and. associated(gfield)) then
1745 this%hm_base%uniform_vector_potential(1:space%periodic_dim) = &
1746 this%hm_base%uniform_vector_potential(1:space%periodic_dim) - time_*this%ep%e_field(1:space%periodic_dim)
1747 end if
1748
1749 end do
1750
1751 ! the vector potential of a static magnetic field
1752 if (allocated(this%ep%a_static)) then
1753 call this%hm_base%allocate_field(mesh, field_vector_potential, .false.)
1754 !ep%a_static contains 1/c A(r)
1755 !$omp parallel do schedule(static) private(idir)
1756 do ip = 1, mesh%np
1757 do idir = 1, space%dim
1758 this%hm_base%vector_potential(idir, ip) = this%hm_base%vector_potential(idir, ip) + this%ep%a_static(ip, idir)
1759 end do
1760 end do
1761 end if
1762
1763 !The electric field is added to the KS potential
1764 call this%hm_base%accel_copy_pot(mesh)
1765
1766 ! and the static magnetic field
1767 if (allocated(this%ep%b_field)) then
1768 call this%hm_base%allocate_field(mesh, field_uniform_magnetic_field, .false.)
1769 do idir = 1, 3
1770 this%hm_base%uniform_magnetic_field(idir) = this%hm_base%uniform_magnetic_field(idir) + this%ep%b_field(idir)
1771 end do
1772 end if
1773
1774 call this%hm_base%update_magnetic_terms(mesh, this%ep%gyromagnetic_ratio, this%d%ispin)
1775
1776 call hamiltonian_elec_update_pot(this, mesh)
1777
1778 call build_phase()
1779
1780 call profiling_out("HAMILTONIAN_ELEC_UPDATE_EXT_POT")
1782
1783 contains
1784
1785 subroutine build_phase()
1786
1788
1789 if ((.not. this%kpoints%gamma_only()) .or. allocated(this%hm_base%uniform_vector_potential)) then
1790
1791 call profiling_in('UPDATE_PHASES')
1792 ! now regenerate the phases for the pseudopotentials
1793 do iatom = 1, this%ep%natoms
1794 call projector_init_phases(this%ep%proj(iatom), space%dim, this%d, this%der%boundaries, this%kpoints, &
1795 vec_pot = this%hm_base%uniform_vector_potential, vec_pot_var = this%hm_base%vector_potential)
1796 end do
1797
1798 call profiling_out('UPDATE_PHASES')
1799 end if
1800
1801 if (allocated(this%hm_base%uniform_vector_potential)) then
1802 call this%phase%update(mesh, this%d%kpt, this%kpoints, this%d, space, this%hm_base%uniform_vector_potential)
1803 end if
1804
1805 call hamiltonian_elec_build_projector_phases(this, this%vnl)
1806 if (this%ep%requires_generalized_overlap) then
1807 call hamiltonian_elec_build_projector_phases(this, this%aug_overlap)
1808 call hamiltonian_elec_build_projector_phases(this, this%aug_inverse_overlap)
1809 ! Exact inverse overlap
1810 if (this%aug_inverse_overlap%ovlp_inv%use_global_coupling) then
1811 if (.not. this%aug_inverse_overlap%ovlp_inv%coupling_built .or. this%phase%is_allocated()) then
1812 call this%aug_inverse_overlap%ovlp_inv%build(this%aug_inverse_overlap%projector_matrices, &
1813 this%aug_inverse_overlap%projector_phases, mesh, this%d%kpt%start, this%d%kpt%end)
1814 end if
1815 end if
1816 end if
1817
1819 end subroutine build_phase
1820
1822
1823 ! ---------------------------------------------------------
1829 subroutine hamiltonian_elec_build_projector_phases(this, nlpp, async)
1830 type(hamiltonian_elec_t), intent(in) :: this
1831 type(nonlocal_pseudopotential_t), intent(inout) :: nlpp
1832 logical, optional, intent(in) :: async
1833
1834 integer :: ik, imat, nmat, max_npoints, offset, iphase, nphase, ip, iatom
1835
1837
1838 if (this%phase%is_allocated() .and. allocated(nlpp%projector_matrices)) then
1839
1840 max_npoints = nlpp%max_npoints
1841 nmat = nlpp%nprojector_matrices
1842
1843 nphase = 1
1844 if (this%der%boundaries%spiralBC) nphase = 3
1845
1846 if (.not. allocated(nlpp%projector_phases)) then
1847 safe_allocate(nlpp%projector_phases(1:max_npoints, 1:nphase, nmat, this%d%kpt%start:this%d%kpt%end))
1848 if (accel_is_enabled()) then
1849 call accel_create_buffer(nlpp%buff_projector_phases, accel_mem_read_only, &
1850 type_cmplx, nlpp%total_points*nphase*this%d%kpt%nlocal())
1851 ! We need to save nphase, with which the array has been build,
1852 ! as the number might change throughout the run
1853 nlpp%nphase = nphase
1854 end if
1855 end if
1856
1857 offset = 0
1858 do ik = this%d%kpt%start, this%d%kpt%end
1859 do imat = 1, nlpp%nprojector_matrices
1860 iatom = nlpp%projector_to_atom(imat)
1861 do iphase = 1, nphase
1862 !$omp parallel do simd schedule(static)
1863 do ip = 1, nlpp%projector_matrices(imat)%npoints
1864 nlpp%projector_phases(ip, iphase, imat, ik) = this%ep%proj(iatom)%phase(ip, iphase, ik)
1865 end do
1866
1867 if (accel_is_enabled() .and. nlpp%projector_matrices(imat)%npoints > 0) then
1868 call accel_write_buffer(nlpp%buff_projector_phases, &
1869 nlpp%projector_matrices(imat)%npoints, nlpp%projector_phases(1:, iphase, imat, ik), &
1870 offset = offset, async=optional_default(async, .false.))
1871 end if
1872 offset = offset + nlpp%projector_matrices(imat)%npoints
1873 end do
1874 end do
1875 end do
1876
1877 end if
1878
1881
1882 logical function hamiltonian_elec_needs_current(hm, states_are_real)
1883 type(hamiltonian_elec_t), intent(in) :: hm
1884 logical, intent(in) :: states_are_real
1885
1887
1888 if (hm%self_induced_magnetic) then
1889 if (.not. states_are_real) then
1891 else
1892 message(1) = 'No current density for real states since it is identically zero.'
1893 call messages_warning(1)
1894 end if
1895 end if
1896
1898
1899 ! ---------------------------------------------------------
1900 subroutine zhamiltonian_elec_apply_all(hm, namespace, gr, st, hst)
1901 type(hamiltonian_elec_t), intent(inout) :: hm
1902 type(namespace_t), intent(in) :: namespace
1903 type(grid_t), intent(in) :: gr
1904 type(states_elec_t), intent(inout) :: st
1905 type(states_elec_t), intent(inout) :: hst
1906
1907 integer :: ik, ib, ist
1908 complex(real64), allocatable :: psi(:, :)
1909 complex(real64), allocatable :: psiall(:, :, :, :)
1910
1912
1913 do ik = st%d%kpt%start, st%d%kpt%end
1914 do ib = st%group%block_start, st%group%block_end
1915 call zhamiltonian_elec_apply_batch(hm, namespace, gr, st%group%psib(ib, ik), hst%group%psib(ib, ik))
1916 end do
1917 end do
1918
1919 if (oct_exchange_enabled(hm%oct_exchange)) then
1920
1921 safe_allocate(psiall(gr%np_part, 1:hst%d%dim, st%st_start:st%st_end, st%d%kpt%start:st%d%kpt%end))
1922
1923 call states_elec_get_state(st, gr, psiall)
1924
1925 call oct_exchange_prepare(hm%oct_exchange, gr, psiall, hm%xc, hm%psolver, namespace)
1926
1927 safe_deallocate_a(psiall)
1928
1929 safe_allocate(psi(gr%np_part, 1:hst%d%dim))
1930
1931 do ik = 1, st%nik
1932 do ist = 1, st%nst
1933 call states_elec_get_state(hst, gr, ist, ik, psi)
1934 call oct_exchange_operator(hm%oct_exchange, namespace, gr, psi, ist, ik)
1935 call states_elec_set_state(hst, gr, ist, ik, psi)
1936 end do
1937 end do
1938
1939 safe_deallocate_a(psi)
1940
1941 end if
1942
1944 end subroutine zhamiltonian_elec_apply_all
1945
1946 ! ---------------------------------------------------------
1947 logical function hamiltonian_elec_has_kick(hm)
1948 type(hamiltonian_elec_t), intent(in) :: hm
1949
1951
1952 hamiltonian_elec_has_kick = (abs(hm%kick%delta_strength) > m_epsilon)
1953
1955 end function hamiltonian_elec_has_kick
1956
1958 !
1959 subroutine hamiltonian_elec_set_mass(this, namespace, mass)
1960 class(hamiltonian_elec_t) , intent(inout) :: this
1961 type(namespace_t), intent(in) :: namespace
1962 real(real64), intent(in) :: mass
1963
1965
1966 if (parse_is_defined(namespace, 'ParticleMass')) then
1967 message(1) = 'Attempting to redefine a non-unit electron mass'
1968 call messages_fatal(1)
1969 else
1970 this%mass = mass
1971 end if
1974 end subroutine hamiltonian_elec_set_mass
1975
1976 ! ---------------------------------------------------------
1977 subroutine hamiltonian_elec_diagonal (hm, mesh, diag, ik)
1978 type(hamiltonian_elec_t), intent(in) :: hm
1979 class(mesh_t), intent(in) :: mesh
1980 real(real64), contiguous, intent(out) :: diag(:,:)
1981 integer, intent(in) :: ik
1982
1983 integer :: idim, ip, ispin
1984
1985 real(real64), allocatable :: ldiag(:)
1986
1988
1989 safe_allocate(ldiag(1:mesh%np))
1990
1991 diag = m_zero
1992
1993 call derivatives_lapl_diag(hm%der, ldiag)
1994
1995 !$omp parallel private(idim, ip)
1996 do idim = 1, hm%d%dim
1997 !$omp do
1998 do ip = 1, mesh%np
1999 diag(ip, idim) = -m_half/hm%mass*ldiag(ip)
2000 end do
2001 end do
2002 !$omp end parallel
2003
2004 select case (hm%d%ispin)
2005
2006 case (unpolarized, spin_polarized)
2007 ispin = hm%d%get_spin_index(ik)
2008 !$omp parallel do
2009 do ip = 1, mesh%np
2010 diag(ip, 1) = diag(ip, 1) + hm%ep%vpsl(ip)
2011 end do
2012 !$omp end parallel do
2013
2014 case (spinors)
2015 do ip = 1, mesh%np
2016 diag(ip, 1) = diag(ip, 1) + hm%ep%vpsl(ip)
2017 diag(ip, 2) = diag(ip, 2) + hm%ep%vpsl(ip)
2018 end do
2020 end select
2021
2022 call hm%ks_pot%add_vhxc(diag)
2023
2025 end subroutine hamiltonian_elec_diagonal
2026
2027
2028
2029#include "undef.F90"
2030#include "real.F90"
2031#include "hamiltonian_elec_inc.F90"
2032
2033#include "undef.F90"
2034#include "complex.F90"
2035#include "hamiltonian_elec_inc.F90"
2036
2037end module hamiltonian_elec_oct_m
2038
2039!! Local Variables:
2040!! mode: f90
2041!! coding: utf-8
2042!! End:
subroutine build_external_potentials()
subroutine build_phase()
subroutine external_potentials_checks()
subroutine build_interactions()
constant times a vector plus a vector
Definition: lalg_basic.F90:171
Copies a vector x, to a vector y.
Definition: lalg_basic.F90:197
integer, parameter, public imaginary_absorbing
subroutine, public absorbing_boundaries_end(this)
subroutine, public absorbing_boundaries_init(this, namespace, space, gr)
pure logical function, public accel_allow_cpu_only()
Definition: accel.F90:401
subroutine, public accel_finish()
Definition: accel.F90:1104
pure logical function, public accel_is_enabled()
Definition: accel.F90:391
This module implements batches of mesh functions.
Definition: batch.F90:112
This module implements common operations on batches of mesh functions.
Definition: batch_ops.F90:95
Module implementing boundary conditions in Octopus.
Definition: boundaries.F90:101
This module calculates the derivatives (gradients, Laplacians, etc.) of a function.
real(real64) function, public derivatives_lapl_get_max_eigenvalue(this)
Get maximum eigenvalue of discrete Laplacian. For the star and star_general stencils,...
subroutine, public energy_copy(ein, eout)
Definition: energy.F90:153
subroutine, public epot_bind_poisson_solver(ep, psolver)
Bind the Poisson solver if the potential manages a density. The Poisson solver pointer is aliased whe...
Definition: epot.F90:486
logical function, public epot_have_external_potentials(ep)
Definition: epot.F90:736
integer, parameter, public scalar_relativistic_zora
Definition: epot.F90:146
subroutine, public epot_end(ep)
Definition: epot.F90:445
integer, parameter, public fully_relativistic_zora
Definition: epot.F90:146
subroutine, public epot_init(ep, namespace, gr, ions, psolver, ispin, xc_family, kpoints)
Definition: epot.F90:210
subroutine, public epot_generate(ep, namespace, mesh, ions, st_d)
Definition: epot.F90:502
subroutine, public exchange_operator_init(this, namespace, space, st, der, mc, stencil, kpoints, cam)
subroutine, public exchange_operator_end(this)
logical function, public list_has_gauge_field(partners)
type(gauge_field_t) function, pointer, public list_get_gauge_field(partners)
logical function, public list_has_lasers(partners)
type(lasers_t) function, pointer, public list_get_lasers(partners)
integer, parameter, public external_pot_from_file
potential, defined in a file
subroutine, public load_external_potentials(external_potentials, namespace)
integer, parameter, public external_pot_charge_density
user-defined function for charge density
integer, parameter, public external_pot_usdef
user-defined function for local potential
integer, parameter, public external_pot_static_efield
Static electric field.
integer, parameter, public external_pot_static_bfield
Static magnetic field.
subroutine, public external_potential_clone(pot_out, pot_in)
Deep-clone an external potential instance.
subroutine, public gauge_field_get_vec_pot(this, vec_pot)
real(real64), parameter, public m_two
Definition: global.F90:175
real(real64), parameter, public m_zero
Definition: global.F90:173
integer, parameter, public rdmft
Definition: global.F90:223
integer, parameter, public hartree_fock
Definition: global.F90:223
integer, parameter, public independent_particles
Theory level.
Definition: global.F90:223
integer, parameter, public generalized_kohn_sham_dft
Definition: global.F90:223
integer, parameter, public kohn_sham_dft
Definition: global.F90:223
real(real64), parameter, public m_epsilon
Definition: global.F90:189
real(real64), parameter, public p_c
Electron gyromagnetic ratio, see Phys. Rev. Lett. 130, 071801 (2023)
Definition: global.F90:215
real(real64), parameter, public m_one
Definition: global.F90:174
This module implements the underlying real-space grid.
Definition: grid.F90:96
This module defines an abstract class for Hamiltonians.
integer, parameter, public field_uniform_magnetic_field
integer, parameter, public field_uniform_vector_potential
integer, parameter, public field_vector_potential
integer, parameter, public term_mgga
subroutine, public hamiltonian_elec_base_accel_rebuild(this, mesh)
Rebuild accelerator buffers after an intrinsic copy.
integer, parameter, public field_potential
subroutine, public zhamiltonian_elec_apply_overlap_single(hm, namespace, mesh, psi, spsi, ist, ik, set_bc, set_phase, inverse)
Apply the overlap S (or its inverse) to a single state (wrapper around the batch routine).
subroutine, public dhamiltonian_elec_apply_sinvh_batch(hm, namespace, mesh, psib, hspsib, terms, set_bc, set_phase)
Apply the operator A = S^{-1} H to a batch (for ultrasoft/PAW).
subroutine, public zhamiltonian_elec_apply_overlap_batch(hm, namespace, mesh, psib, spsib, set_bc, inverse, set_phase)
Apply the generalized ultrasoft overlap operator S (or its inverse) to a batch.
pure logical function, public hamiltonian_elec_apply_packed(this)
subroutine, public hamiltonian_elec_set_inh(hm, st)
subroutine, public zvmask(mesh, hm, st)
subroutine, public dhamiltonian_elec_apply_batch(hm, namespace, mesh, psib, hpsib, terms, set_bc, set_phase, spsib)
subroutine, public hamiltonian_elec_adjoint(hm)
subroutine, public hamiltonian_elec_end(hm)
subroutine, public zhamiltonian_elec_apply_single(hm, namespace, mesh, psi, hpsi, ist, ik, terms, set_bc, set_phase)
pure logical function hamiltonian_elec_needs_mgga_term(hm, terms)
logical function, public hamiltonian_elec_has_kick(hm)
integer, parameter, public length
subroutine hamiltonian_elec_build_projector_phases(this, nlpp, async)
Build the projector phases for a given nonlocal-pseudopotential set.
logical function hamiltonian_elec_hermitian(hm)
subroutine, public dhamiltonian_elec_apply_single(hm, namespace, mesh, psi, hpsi, ist, ik, terms, set_bc, set_phase)
subroutine, public hamiltonian_elec_epot_generate(this, namespace, space, gr, ions, ext_partners, st, time)
real(real64) function, public hamiltonian_elec_get_time(this)
subroutine, public zhamiltonian_elec_apply_batch(hm, namespace, mesh, psib, hpsib, terms, set_bc, set_phase, spsib)
subroutine, public hamiltonian_elec_copy(hm_out, hm_in)
Deep-copy a hamiltonian_elec_t snapshot.
subroutine, public zhamiltonian_elec_apply_sinvh_single(hm, namespace, mesh, psi, hspsi, ist, ik, terms, set_bc, set_phase)
Apply A = S^{-1} H to a single state (wrapper around the batch routine).
subroutine, public dhamiltonian_elec_apply_sinvh_single(hm, namespace, mesh, psi, hspsi, ist, ik, terms, set_bc, set_phase)
Apply A = S^{-1} H to a single state (wrapper around the batch routine).
subroutine, public dhamiltonian_elec_apply_overlap_batch(hm, namespace, mesh, psib, spsib, set_bc, inverse, set_phase)
Apply the generalized ultrasoft overlap operator S (or its inverse) to a batch.
subroutine, public dvmask(mesh, hm, st)
logical function, public hamiltonian_elec_needs_current(hm, states_are_real)
subroutine, public hamiltonian_elec_remove_inh(hm)
subroutine, public dhamiltonian_elec_apply_overlap_single(hm, namespace, mesh, psi, spsi, ist, ik, set_bc, set_phase, inverse)
Apply the overlap S (or its inverse) to a single state (wrapper around the batch routine).
subroutine, public zhamiltonian_elec_apply_atom(hm, namespace, space, latt, species, pos, ia, mesh, psi, vpsi)
subroutine hamiltonian_elec_release_copy_owned(hm)
Release owned pointer targets created by hamiltonian_elec_copy.
subroutine, public zhamiltonian_elec_apply_all(hm, namespace, gr, st, hst)
subroutine, public zhamiltonian_elec_apply_sinvh_batch(hm, namespace, mesh, psib, hspsib, terms, set_bc, set_phase)
Apply the operator A = S^{-1} H to a batch (for ultrasoft/PAW).
subroutine, public hamiltonian_elec_diagonal(hm, mesh, diag, ik)
subroutine, public hamiltonian_elec_update_pot(this, mesh, accumulate)
Update the KS potential of the electronic Hamiltonian.
integer, parameter, public velocity
subroutine hamiltonian_elec_update(this, mesh, namespace, space, ext_partners, time)
(re-)build the Hamiltonian for the next application:
subroutine hamiltonian_elec_copy_guard_supported(hm_in)
Hard-fail guard matrix for unsupported hamiltonian snapshots.
subroutine hamiltonian_elec_span(hm, delta, emin, namespace)
subroutine, public hamiltonian_elec_init(hm, namespace, space, gr, ions, ext_partners, st, theory_level, xc, mc, kpoints, need_exchange, xc_photons)
subroutine dhamiltonian_elec_apply(hm, namespace, mesh, psib, hpsib, terms, set_bc)
pure logical function, public hamiltonian_elec_inh_term(hm)
subroutine hamiltonian_elec_set_mass(this, namespace, mass)
set the effective electron mass, checking whether it was previously redefined.
subroutine, public hamiltonian_elec_update_with_ext_pot(this, mesh, space, ext_partners, time, mu)
This is an extension of "hamiltonian_elec_update_pot" to be used by the CFM4 propagator....
subroutine, public hamiltonian_elec_not_adjoint(hm)
subroutine, public zhamiltonian_elec_external(this, mesh, psib, vpsib)
subroutine zhamiltonian_elec_apply(hm, namespace, mesh, psib, hpsib, terms, set_bc)
This module defines classes and functions for interaction partners.
Definition: io.F90:93
subroutine, public kick_copy(kick_out, kick_in)
Definition: kick.F90:733
integer, parameter, public kick_magnon_mode
Definition: kick.F90:142
subroutine, public kick_end(kick)
Definition: kick.F90:776
subroutine, public kick_init(kick, namespace, space, kpoints, nspin)
Definition: kick.F90:202
pure integer function, public kick_get_type(kick)
Definition: kick.F90:1354
A module to handle KS potential, without the external potential.
subroutine, public ks_potential_accel_rebuild(this)
Rebuild accelerator buffers after an intrinsic copy.
subroutine, public laser_vector_potential(laser, mesh, aa, time)
Definition: lasers.F90:1065
subroutine, public lasers_nondipole_laser_field_step(this, field, time)
Retrieves the NDSFA vector_potential correction. The nondipole field is obtained for consecutive time...
Definition: lasers.F90:1137
logical function, public lasers_with_nondipole_field(lasers)
Check if a nondipole SFA correction should be computed for the given laser.
Definition: lasers.F90:724
integer, parameter, public e_field_electric
Definition: lasers.F90:158
integer, parameter, public e_field_vector_potential
Definition: lasers.F90:158
subroutine, public laser_potential(laser, mesh, pot, time)
Definition: lasers.F90:1030
integer, parameter, public e_field_scalar_potential
Definition: lasers.F90:158
integer pure elemental function, public laser_kind(laser)
Definition: lasers.F90:700
subroutine, public laser_field(laser, field, time)
Retrieves the value of either the electric or the magnetic field. If the laser is given by a scalar p...
Definition: lasers.F90:1102
integer, parameter, public e_field_magnetic
Definition: lasers.F90:158
integer, parameter, public dft_u_none
Definition: lda_u.F90:188
subroutine, public lda_u_init(this, namespace, space, level, gr, ions, st, mc, kpoints)
Definition: lda_u.F90:270
subroutine, public lda_u_accel_rebuild(this, kpt)
Rebuild DFT+U accelerator buffers after intrinsic assignment.
Definition: lda_u.F90:758
subroutine, public lda_u_rebind_after_copy(this, ions)
Rebind non-owning pointers after intrinsic assignment of lda_u_t.
Definition: lda_u.F90:713
subroutine, public lda_u_build_phase_correction(this, space, std, boundaries, namespace, kpoints, vec_pot, vec_pot_var)
Build the phase correction to the global phase for all orbitals.
Definition: lda_u.F90:942
subroutine, public lda_u_end(this)
Definition: lda_u.F90:667
subroutine, public lda_u_update_basis(this, space, gr, ions, st, psolver, namespace, kpoints, has_phase, aug_overlap)
Definition: lda_u.F90:801
This module implements fully polymorphic linked lists, and some specializations thereof.
Definition: linked_list.F90:84
This modules implements the routines for doing constrain DFT for noncollinear magnetism.
subroutine, public magnetic_constrain_copy(this_out, this_in)
Deep-copy magnetic constrain data.
subroutine, public magnetic_constrain_end(this)
Releases memory of the magnetic constrain.
subroutine, public magnetic_constrain_init(this, namespace, mesh, std, natoms, min_dist)
Initilializes the magnetic_constrain_t object.
This module is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:94
This module defines various routines, operating on mesh functions.
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:97
subroutine, public messages_not_implemented(feature, namespace)
Definition: messages.F90:1048
subroutine, public messages_warning(no_lines, all_nodes, namespace)
Definition: messages.F90:501
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:138
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:386
subroutine, public messages_experimental(name, namespace)
Definition: messages.F90:1020
This module handles the communicators for the various parallelization strategies.
Definition: multicomm.F90:124
subroutine, public mxll_coupling_init(this, d, gr, namespace, mass)
Parse variables and initialize Maxwell coupling.
subroutine, public set_electric_quadrupole_pot(this, mesh)
Computes the electric quadrupole potential where .
subroutine, public mxll_coupling_end(this)
Finalize and deallocate Maxwell coupling arrays.
subroutine, public mxll_coupling_calc(this, hm_base, mesh, d, space)
Add the Maxwell coupling to the electronic Hamiltonian.
subroutine, public mxll_coupling_copy(this_out, this_in, der_target)
Deep-copy Maxwell-electron coupling data and rebind derivatives.
subroutine, public nonlocal_pseudopotential_accel_rebuild(this, space, mesh)
Rebuild accelerator buffers after an intrinsic copy.
subroutine, public nonlocal_pseudopotential_rebind_projectors(this, epot)
Rebind projector matrix pointers (map, position) to a target epot.
subroutine, public nonlocal_pseudopotential_update_dij(this, mesh, epot, veff, add_bare, onsite_density_matrix)
Screen the ultrasoft nonlocal coupling matrix D_ij with the local potential.
subroutine, public nonlocal_pseudopotential_build_paw_symm(this, epot, space, gr, ions)
Build the crystal-symmetry data used to symmetrize the PAW one-center density matrix.
logical function, public oct_exchange_enabled(this)
subroutine, public oct_exchange_remove(this)
this module contains the low-level part of the output system
Definition: output_low.F90:94
Some general things and nomenclature:
Definition: par_vec.F90:150
logical function, public parse_is_defined(namespace, name)
Definition: parser.F90:440
subroutine, public pcm_calc_pot_rs(pcm, mesh, psolver, ions, v_h, v_ext, kick, time_present, kick_time)
Definition: pcm.F90:1205
subroutine, public pcm_end(pcm)
Definition: pcm.F90:3059
subroutine, public pcm_init(pcm, namespace, space, ions, grid, qtot, val_charge, external_potentials_present, kick_present)
Initializes the PCM calculation: reads the VdW molecular cavity and generates the PCM response matrix...
Definition: pcm.F90:277
subroutine, public phase_accel_rebuild(phase, mesh, kpt)
Rebuild phase accelerator buffers after an intrinsic copy.
Definition: phase.F90:518
subroutine, public poisson_init(this, namespace, space, der, mc, stencil, qtot, label, solver, verbose, force_serial, force_cmplx, fft_library, fft_batch_size, single_precision)
Definition: poisson.F90:223
subroutine, public poisson_end(this)
Definition: poisson.F90:712
/* To use the profiling module, call profiling_in and profiling_out with the label of the region as a...
Definition: profiling.F90:122
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
Definition: profiling.F90:672
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
Definition: profiling.F90:604
subroutine, public projector_init_phases(this, dim, std, bnd, kpoints, vec_pot, vec_pot_var)
Definition: projector.F90:270
subroutine, public scissor_end(this)
Definition: scissor.F90:226
subroutine, public states_set_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)
subroutine, public states_elec_end(st)
finalize the states_elec_t object
subroutine, public states_elec_copy(stout, stin, exclude_wfns, exclude_eigenval, special, dest_type, copy_data)
make a (selective) copy of a states_elec_t object
This module provides routines for communicating states when using states parallelization.
integer pure function, public symmetries_identity_index(this)
Definition: symmetries.F90:614
integer pure function, public symmetries_number(this)
Definition: symmetries.F90:568
brief This module defines the class unit_t which is used by the unit_systems_oct_m module.
Definition: unit.F90:111
This module defines the unit system, used for input and output.
type(unit_system_t), public units_inp
the units systems for reading and writing
type(xc_cam_t), parameter, public cam_exact_exchange
Use only Hartree Fock exact exchange.
Definition: xc_cam.F90:131
integer, parameter, public xc_oep_x_slater
Slater approximation to the exact exchange.
integer, parameter, public func_x
Definition: xc.F90:97
logical pure function, public family_is_mgga_with_exc(xcs)
Is the xc function part of the mGGA family with an energy functional.
Definition: xc.F90:713
logical pure function, public family_is_hybrid(xcs)
Returns true if the functional is an hybrid functional.
Definition: xc.F90:728
This module implements the "photon-free" electron-photon exchange-correlation functional.
Definition: xc_photons.F90:100
This module implements the ZORA terms for the Hamoiltonian.
Definition: zora.F90:95
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:148
The abstract Hamiltonian class defines a skeleton for specific implementations.
abstract class for general interaction partners
Describes mesh distribution to nodes.
Definition: mesh.F90:165
Stores all communicators and groups.
Definition: multicomm.F90:185
The states_elec_t class contains all electronic wave functions.
This class described the 'photon-exchange' electron-photon xc functionals, based on QEDFT.
Definition: xc_photons.F90:137
This class is responsible for calculating and applying the ZORA.
Definition: zora.F90:124
int true(void)