Octopus
rdmft.F90
Go to the documentation of this file.
1!! Copyright (C) 2012-2019 I. Theophilou, N. Helbig
2!! Copyright (C) 2019 F. Buchholz, M. Oliveira
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
22module rdmft_oct_m
23 use debug_oct_m
30 use energy_oct_m
32 use global_oct_m
33 use grid_oct_m
37 use io_oct_m
39 use ions_oct_m
40 use, intrinsic :: iso_fortran_env
42 use loct_oct_m
43 use math_oct_m
44 use mesh_oct_m
48 use mpi_oct_m
52 use output_oct_m
55 use parser_oct_m
60 use space_oct_m
65 use unit_oct_m
67 use v_ks_oct_m
68 use xc_oep_oct_m
69
70 implicit none
71
72 private
73 public :: &
74 rdm_t, &
75 rdmft_init, &
76 rdmft_end, &
78
79 type rdm_t
80 private
81 type(eigensolver_t) :: eigens
82 integer :: max_iter
83 integer :: iter
84 integer :: nst
85 integer :: n_twoint !number of unique two electron integrals
86 logical :: do_basis
87 logical :: hf
88 real(real64) :: mu
89 real(real64) :: occsum
90 real(real64) :: qtot
91 real(real64) :: scale_f
92 real(real64) :: toler
93 real(real64) :: conv_ener
94 real(real64) :: maxFO
95 real(real64) :: tolerFO
96
97 real(real64), allocatable :: eone(:)
98 real(real64), allocatable :: eone_int(:,:)
99 real(real64), allocatable :: twoint(:)
100 real(real64), allocatable :: hartree(:,:)
101 real(real64), allocatable :: exchange(:,:)
102 real(real64), allocatable :: evalues(:)
103 real(real64), allocatable :: vecnat(:,:)
104 real(real64), allocatable :: Coul(:,:,:)
105 real(real64), allocatable :: Exch(:,:,:)
106
107 integer, allocatable :: i_index(:,:)
108 integer, allocatable :: j_index(:,:)
109 integer, allocatable :: k_index(:,:)
110 integer, allocatable :: l_index(:,:)
111
112 type(elec_matrix_elements_t) :: elec_me
113 end type rdm_t
114
115 type(rdm_t), pointer :: rdm_ptr
116
117contains
118
119 ! ---------------------------------------------------------
120 subroutine rdmft_init(rdm, namespace, gr, st, mc, space, fromScratch)
121 type(rdm_t), intent(out) :: rdm
122 type(namespace_t), intent(in) :: namespace
123 type(grid_t), intent(inout) :: gr
124 type(states_elec_t), intent(in) :: st
125 type(multicomm_t), intent(in) :: mc
126 class(space_t), intent(in) :: space
127 logical, intent(in) :: fromScratch
128
129 push_sub(rdmft_init)
130
131 if(st%nst < st%qtot + 1) then
132 message(1) = "Too few states to run RDMFT calculation"
133 message(2) = "Number of states should be at least the number of electrons plus one"
134 call messages_fatal(2, namespace=namespace)
135 end if
136
137 if (states_are_complex(st)) then
138 call messages_not_implemented("Complex states for RDMFT", namespace=namespace)
139 end if
140
141 ! The documentation for the variable is found in scf_init.
142 call parse_variable(namespace, 'MaximumIter', 200, rdm%max_iter)
143
144 !%Variable RDMTolerance
145 !%Type float
146 !%Default 1e-7 Ha
147 !%Section SCF::RDMFT
148 !%Description
149 !% Convergence criterion for stopping the occupation numbers minimization. Minimization is
150 !% stopped when all derivatives of the energy wrt. each occupation number
151 !% are smaller than this criterion. The bisection for finding the correct mu that is needed
152 !% for the occupation number minimization also stops according to this criterion.
153 !%End
154 call parse_variable(namespace, 'RDMTolerance', 1.0e-7_real64, rdm%toler)
155
156 !%Variable RDMToleranceFO
157 !%Type float
158 !%Default 1e-4 Ha
159 !%Section SCF::RDMFT
160 !%Description
161 !% Convergence criterion for stopping the diagonalization of the Fock matrix in the Piris method.
162 !% Orbital minimization is stopped when all off-diagonal ellements of the Fock matrix
163 !% are smaller than this criterion.
164 !%End
165 call parse_variable(namespace, 'RDMToleranceFO', 1.0e-4_real64, rdm%tolerFO)
166
167 !%Variable RDMConvEner
168 !%Type float
169 !%Default 1e-6 Ha
170 !%Section SCF::RDMFT
171 !%Description
172 !% Convergence criterion for stopping the overall minimization of the energy with
173 !% respect to occupation numbers and the orbitals. The minimization of the
174 !% energy stops when the total energy difference between two subsequent
175 !% minimizations of the energy with respect to the occupation numbers and the
176 !% orbitals is smaller than this criterion. It is also used to exit the orbital minimization.
177 !%End
178 call parse_variable(namespace, 'RDMConvEner', 1.0e-7_real64, rdm%conv_ener)
180 !%Variable RDMBasis
181 !%Type logical
182 !%Default yes
183 !%Section SCF::RDMFT
184 !%Description
185 !% If true, all the energy terms and corresponding derivatives involved in RDMFT will
186 !% not be calculated on the grid but on the basis of the initial orbitals
187 !%End
188 call parse_variable(namespace, 'RDMBasis',.true., rdm%do_basis)
189
190 if (rdm%do_basis .and. fromscratch) then
191 call messages_write("RDMFT calculations with RDMBasis = yes cannot be started FromScratch", new_line=.true.)
192 call messages_write("Run a calculation for independent particles first")
193 call messages_fatal(namespace=namespace)
194 end if
196 !%Variable RDMHartreeFock
197 !%Type logical
198 !%Default no
199 !%Section SCF::RDMFT
200 !%Description
201 !% If true, the code simulates a HF calculation, by omitting the occ.num. optimization
202 !% can be used for test reasons
203 !%End
204 call parse_variable(namespace, 'RDMHartreeFock',.false., rdm%hf)
206 rdm%nst = st%nst
207 if (rdm%do_basis) then
208 rdm%n_twoint = rdm%nst*(rdm%nst + 1)*(rdm%nst**2 + rdm%nst + 2)/8
209 safe_allocate(rdm%eone_int(1:rdm%nst, 1:rdm%nst))
210 safe_allocate(rdm%twoint(1:rdm%n_twoint))
211 safe_allocate(rdm%i_index(1:2,1:rdm%n_twoint))
212 safe_allocate(rdm%j_index(1:2,1:rdm%n_twoint))
213 safe_allocate(rdm%k_index(1:2,1:rdm%n_twoint))
214 safe_allocate(rdm%l_index(1:2,1:rdm%n_twoint))
215 safe_allocate(rdm%vecnat(1:rdm%nst, 1:rdm%nst))
216 safe_allocate(rdm%Coul(1:rdm%nst, 1:rdm%nst, 1:rdm%nst))
217 safe_allocate(rdm%Exch(1:rdm%nst, 1:rdm%nst, 1:rdm%nst))
218 rdm%eone_int = m_zero
219 rdm%twoint = m_zero
220 rdm%vecnat = diagonal_matrix(rdm%nst, m_one)
221 rdm%i_index = m_zero
222 rdm%j_index = m_zero
223 rdm%k_index = m_zero
224 rdm%l_index = m_zero
225 rdm%Coul = m_zero
226 rdm%Exch = m_zero
227 else
228 ! initialize eigensolver.
229 call eigensolver_init(rdm%eigens, namespace, gr, st, mc, space)
230 if (rdm%eigens%additional_terms) call messages_not_implemented("CG Additional Terms with RDMFT", namespace=namespace)
231 end if
232
233 safe_allocate(rdm%eone(1:rdm%nst))
234 safe_allocate(rdm%hartree(1:rdm%nst, 1:rdm%nst))
235 safe_allocate(rdm%exchange(1:rdm%nst, 1:rdm%nst))
236 safe_allocate(rdm%evalues(1:rdm%nst))
237
238 rdm%eone = m_zero
239 rdm%hartree = m_zero
240 rdm%exchange = m_zero
241 rdm%evalues = m_zero
242 rdm%mu = m_two*st%eigenval(int(st%qtot*m_half), 1)
243 rdm%qtot = st%qtot
244 rdm%occsum = m_zero
245 rdm%scale_f = 1e-2_real64
246 rdm%maxFO = m_zero
247 rdm%iter = 0
248
249 ! Initialize matrix element calculator
250 call rdm%elec_me%init(gr, space, st)
251
252 pop_sub(rdmft_init)
253 end subroutine rdmft_init
254
255 ! ----------------------------------------
256
257 subroutine rdmft_end(rdm)
258 type(rdm_t), intent(inout) :: rdm
259
260 push_sub(rdmft_end)
261
262 safe_deallocate_a(rdm%evalues)
263 safe_deallocate_a(rdm%eone)
264 safe_deallocate_a(rdm%hartree)
265 safe_deallocate_a(rdm%exchange)
266
267 if (rdm%do_basis) then
268 safe_deallocate_a(rdm%eone_int)
269 safe_deallocate_a(rdm%twoint)
270 safe_deallocate_a(rdm%i_index)
271 safe_deallocate_a(rdm%j_index)
272 safe_deallocate_a(rdm%k_index)
273 safe_deallocate_a(rdm%l_index)
274 safe_deallocate_a(rdm%vecnat)
275 safe_deallocate_a(rdm%Coul)
276 safe_deallocate_a(rdm%Exch)
277 else
278 call eigensolver_end(rdm%eigens)
279 end if
280
281 pop_sub(rdmft_end)
282 end subroutine rdmft_end
283
284 ! ----------------------------------------
285
286 ! scf for the occupation numbers and the natural orbitals
287 subroutine scf_rdmft(rdm, namespace, space, gr, ions, ext_partners, st, ks, hm, outp, restart_dump)
288 type(rdm_t), intent(inout) :: rdm
289 type(namespace_t), intent(in) :: namespace
290 type(electron_space_t), intent(in) :: space
291 type(grid_t), intent(in) :: gr
292 type(ions_t), intent(in) :: ions
293 type(partner_list_t), intent(in) :: ext_partners
294 type(states_elec_t), intent(inout) :: st
295 type(v_ks_t), intent(inout) :: ks
296 type(hamiltonian_elec_t), intent(inout) :: hm
297 type(output_t), intent(in) :: outp
298 type(restart_t), intent(in) :: restart_dump
299
300 type(states_elec_t) :: states_save
301 integer :: iter, icount, ip, ist, ierr, maxcount, iorb
302 integer(int64) :: what_i
303 real(real64) :: energy, energy_dif, energy_old, energy_occ, xpos, xneg, rel_ener
304 real(real64), allocatable :: dpsi(:,:), dpsi2(:,:)
305 logical :: conv
306 character(len=MAX_PATH_LEN) :: dirname
307
308 push_sub(scf_rdmft)
309
310 if (hm%d%ispin /= 1) then
311 call messages_not_implemented("RDMFT exchange function not yet implemented for spin_polarized or spinors", &
312 namespace=namespace)
313 end if
314
315 ! problem is about k-points for exchange
316 if (space%is_periodic()) then
317 call messages_not_implemented("Periodic system calculations for RDMFT", namespace=namespace)
318 end if
319
320 ! exchange routine needs all states on each processor currently
321 if(st%parallel_in_states) then
322 call messages_not_implemented("RDMFT parallel in states", namespace=namespace)
323 end if
324
325 call messages_print_with_emphasis(msg='RDMFT Calculation', namespace=namespace)
326 call messages_print_var_value('RDMBasis', rdm%do_basis, namespace=namespace)
327
328 !set initial values
329 energy_old = 1.0e20_real64
330 xpos = m_zero
331 xneg = m_zero
332 energy = m_zero
333 if (.not. rdm%do_basis) then
334 maxcount = 1 !still needs to be checked
335 else
336 maxcount = 50
337 !precalculate matrix elements in basis
338 write(message(1),'(a)') 'Calculating Coulomb and exchange matrix elements in basis'
339 write(message(2),'(a)') '--this may take a while--'
340 call messages_info(2, namespace=namespace)
341
342 call rdm%elec_me%two_body_me(namespace, hm%kpoints, hm%exxop%psolver, 1, st%nst, rdm%i_index, rdm%j_index, rdm%k_index, &
343 rdm%l_index, rdm%twoint)
344 call rdm_integrals(rdm, namespace, hm, st, gr)
345 call sum_integrals(rdm)
346 endif
347
348 ! Start the actual minimization, first step is minimization of occupation numbers
349 ! Orbital minimization is according to Piris and Ugalde, Vol. 30, No. 13, J. Comput. Chem. (scf_orb) or
350 ! using conjugated gradient (scf_orb_cg)
351 do iter = 1, rdm%max_iter
352 rdm%iter = rdm%iter + 1
353 write(message(1), '(a)') '**********************************************************************'
354 write(message(2),'(a, i4)') 'Iteration:', iter
355 call messages_info(2, namespace=namespace)
356 ! occupation number optimization unless we are doing Hartree-Fock
357 if (rdm%hf) then
358 call scf_occ_no(rdm, namespace, gr, hm, space, st, energy_occ)
359 else
360 call scf_occ(rdm, namespace, gr, hm, space, st, energy_occ)
361 end if
362 ! orbital optimization
363 write(message(1), '(a)') 'Optimization of natural orbitals'
364 call messages_info(1, namespace=namespace)
365 do icount = 1, maxcount
366 if (rdm%do_basis) then
367 call scf_orb(rdm, namespace, gr, st, hm, space, energy)
368 else
369 call scf_orb_cg(rdm, namespace, space, gr, ions, ext_partners, st, ks, hm, energy)
370 end if
371 energy_dif = energy - energy_old
372 energy_old = energy
373 if (rdm%do_basis) then
374 if (abs(energy_dif)/abs(energy) < rdm%conv_ener .and. rdm%maxFO < rdm%tolerFO) exit
375 if (energy_dif < m_zero) then
376 xneg = xneg + 1
377 else
378 xpos = xpos + 1
379 end if
380 if (xneg > 1.5e0_real64*xpos) then
381 rdm%scale_f = 1.01_real64*rdm%scale_f
382 elseif (xneg < 1.1e0_real64*xpos) then
383 rdm%scale_f = 0.95_real64* rdm%scale_f
384 end if
385 endif !rdm%do_basis
386 end do !icount
387 xneg = m_zero
388 xpos = m_zero
389
390 rel_ener = abs(energy_occ-energy)/abs(energy)
391
392 write(message(1),'(a,11x,es20.10)') 'Total energy:', units_from_atomic(units_out%energy,energy + hm%ep%eii)
393 write(message(2),'(a,1x,es20.10)') 'Rel. energy difference:', rel_ener
394 call messages_info(2, namespace=namespace)
395
396 if (.not. rdm%hf .and. rdm%do_basis) then
397 write(message(1),'(a,18x,es20.10)') 'Max F0:', rdm%maxFO
398 call messages_info(1, namespace=namespace)
399 end if
400
401
402 if (rdm%do_basis) then
403 conv = (rel_ener < rdm%conv_ener) .and. rdm%maxFO < rdm%tolerFO
404 else
405 conv = rel_ener < rdm%conv_ener
406 endif
407
408 if (rdm%toler > 1e-4_real64) rdm%toler = rdm%toler*1e-1_real64 !Is this still okay or does it restrict the possible convergence? FB: Does this makes sense at all?
409
410 ! save restart information
411 if ((conv .or. (modulo(iter, outp%restart_write_interval) == 0) .or. iter == rdm%max_iter)) then
412 if (rdm%do_basis) then
413 call states_elec_copy(states_save, st)
414 safe_allocate(dpsi(1:gr%np, 1:st%d%dim))
415 safe_allocate(dpsi2(1:gr%np, 1:st%d%dim))
416 do iorb = 1, st%nst
417 dpsi = m_zero
418 do ist = 1, st%nst
419 call states_elec_get_state(st, gr, ist, 1, dpsi2)
420 do ip = 1, gr%np
421 dpsi(ip,1) = dpsi(ip,1) + rdm%vecnat(ist, iorb)*dpsi2(ip,1)
422 end do
423 end do
424 call states_elec_set_state(states_save, gr, iorb, 1, dpsi)
425 end do
426 call density_calc(states_save, gr, states_save%rho)
427 ! if other quantities besides the densities and the states are needed they also have to be recalculated here!
428 call states_elec_dump(restart_dump, space, states_save, gr, hm%kpoints, ierr, iter=iter)
429
430 if (conv .or. iter == rdm%max_iter) then
431 call states_elec_end(st)
432 call states_elec_copy(st, states_save)
433 end if
434
435 call states_elec_end(states_save)
436
437 safe_deallocate_a(dpsi)
438 safe_deallocate_a(dpsi2)
439 else
440 call states_elec_dump(restart_dump, space, st, gr, hm%kpoints, ierr, iter=iter)
441
442 ! calculate maxFO for cg-solver
443 if (.not. rdm%hf) then
444 call calc_maxfo (namespace, hm, st, gr, rdm)
445 write(message(1),'(a,18x,es20.10)') 'Max F0:', rdm%maxFO
446 call messages_info(1, namespace=namespace)
447 end if
448 endif
449
450 if (ierr /= 0) then
451 message(1) = 'Unable to write states wavefunctions.'
452 call messages_warning(1, namespace=namespace)
453 end if
454
455 endif
456
457 ! write output for iterations if requested
458 if (any(outp%what) .and. outp%duringscf) then
459 do what_i = lbound(outp%what, 1), ubound(outp%what, 1)
460 if (outp%what_now(what_i, iter)) then
461 write(dirname,'(a,a,i4.4)') trim(outp%iter_dir), "scf.", iter
462 call output_all(outp, namespace, space, dirname, gr, ions, iter, st, hm, ks)
463 call output_modelmb(outp, namespace, space, dirname, gr, ions, iter, st)
464 call scf_write_static(dirname, "info")
465 exit
466 end if
467 end do
468 end if
469
470 if (conv) exit
471 end do
472
473 if(conv) then
474 write(message(1),'(a,i3,a)') 'The calculation converged after ',rdm%iter,' iterations'
475 write(message(2),'(a,9x,es20.10)') 'The total energy is ', units_from_atomic(units_out%energy,energy + hm%ep%eii)
476 call messages_info(2, namespace=namespace)
477 else
478 write(message(1),'(a,i3,a)') 'The calculation did not converge after ', iter-1, ' iterations '
479 write(message(2),'(a,es15.5)') 'Relative energy difference between the last two iterations ', rel_ener
480 write(message(3),'(a,es15.5)') 'The maximal non-diagonal element of the Hermitian matrix F is ', rdm%maxFO
481 call messages_info(3, namespace=namespace)
482 end if
483
484 call scf_write_static(static_dir, "info")
485 call output_all(outp, namespace, space, static_dir, gr, ions, -1, st, hm, ks)
486 call output_modelmb(outp, namespace, space, static_dir, gr, ions, -1, st)
487
488 pop_sub(scf_rdmft)
489
490 contains
491 ! ---------------------------------------------------------
492 subroutine scf_write_static(dir, fname)
493 character(len=*), intent(in) :: dir, fname
494
495 integer :: iunit, ist
496 real(real64), allocatable :: photon_number_state (:), ekin_state (:), epot_state (:)
497
499
500 safe_allocate(photon_number_state(1:st%nst))
501 safe_allocate(ekin_state(1:st%nst))
502 safe_allocate(epot_state(1:st%nst))
503
504 if(mpi_grp_is_root(mpi_world)) then
505 call io_mkdir(dir, namespace)
506 iunit = io_open(trim(dir) // "/" // trim(fname), namespace, action='write')
507
508 call grid_write_info(gr, iunit=iunit)
509
510 call v_ks_write_info(ks, iunit=iunit)
511
512 if (rdm%do_basis) then
513 write(iunit, '(a)')'Orbital optimization with [basis set]'
514 else
515 write(iunit, '(a)')'Orbital optimization with [conjugated gradients]'
516 end if
517 write(iunit, '(1x)')
518
519 if (rdm%hf) then
520 write(iunit, '(a)')'Hartree Fock calculation'
521 write(iunit, '(1x)')
522 end if
523
524 if (hm%psolver%is_dressed) then
525 write(iunit, '(a)')'Dressed state calculation'
526 call photon_mode_write_info(hm%psolver%photons, iunit=iunit)
527 write(iunit, '(1x)')
528 end if
529
530 ! scf information
531 if(conv) then
532 write(iunit, '(a, i4, a)')'SCF converged in ', iter, ' iterations'
533 else
534 write(iunit, '(a)') 'SCF *not* converged!'
535 end if
536 write(iunit, '(1x)')
537
538 write(iunit, '(3a,es20.10)') 'Total Energy [', trim(units_abbrev(units_out%energy)), ']:', &
539 units_from_atomic(units_out%energy, energy + hm%ep%eii)
540 write(iunit,'(a,1x,f16.12)') 'Sum of occupation numbers:', rdm%occsum
541 else
542 iunit = 0
543 end if
544
545 if (hm%psolver%is_dressed) then
546 call calc_photon_number(space, gr, st, hm%psolver%photons, photon_number_state, ekin_state, epot_state)
547 if(mpi_grp_is_root(mpi_world)) then
548 write(iunit,'(a,1x,f14.12)') 'Total mode occupation:', hm%psolver%photons%number(1)
549 end if
550 end if
551
552 if(mpi_grp_is_root(mpi_world)) then
553 if (rdm%max_iter > 0) then
554 write(iunit, '(a)') 'Convergence:'
555 write(iunit, '(6x, a, es15.8,a,es15.8,a)') 'maxFO = ', rdm%maxFO
556 write(iunit, '(6x, a, es15.8,a,es15.8,a)') 'rel_ener = ', rel_ener
557 write(iunit,'(1x)')
558 end if
559 ! otherwise, these values are uninitialized, and unknown.
560 end if
561
562 if (mpi_grp_is_root(mpi_world)) then
563 ! Write header
564 write(iunit,'(a)') 'Natural occupation numbers:'
565 write(iunit,'(a4,5x,a12)', advance='no') '#st', 'Occupation'
566 if (.not. rdm%do_basis) write(iunit,'(5x,a12)', advance='no') 'conv'
567 if (hm%psolver%is_dressed) write(iunit,'(3(5x,a12))', advance='no') 'Mode Occ.', '-1/2d^2/dq^2', '1/2w^2q^2'
568 write(iunit,*)
569
570 ! Write values
571 do ist = 1, st%nst
572 write(iunit,'(i4,3x,f14.12)', advance='no') ist, st%occ(ist, 1)
573 if (.not. rdm%do_basis) write(iunit,'(3x,f14.12)', advance='no') rdm%eigens%diff(ist, 1)
574 if (hm%psolver%is_dressed) then
575 write(iunit,'(3(3x,f14.12))', advance='no') photon_number_state(ist), ekin_state(ist), epot_state(ist)
576 end if
577 write(iunit,*)
578 end do
579 end if
580
581 if (mpi_grp_is_root(mpi_world)) then
582 call io_close(iunit)
583 end if
584
585 safe_deallocate_a(photon_number_state)
586 safe_deallocate_a(ekin_state)
587 safe_deallocate_a(epot_state)
588
590 end subroutine scf_write_static
591 end subroutine scf_rdmft
592
593 ! ---------------------------------------------------------
594 subroutine calc_maxfo (namespace, hm, st, gr, rdm)
595 type(namespace_t), intent(in) :: namespace
596 type(rdm_t), intent(inout) :: rdm
597 type(grid_t), intent(in) :: gr
598 type(hamiltonian_elec_t), intent(inout) :: hm
599 type(states_elec_t), intent(inout) :: st
600
601 real(real64), allocatable :: lambda(:,:), FO(:,:)
602 integer :: ist, jst
603
604 push_sub(calc_maxfo)
605
606 safe_allocate(lambda(1:st%nst,1:st%nst))
607 safe_allocate(fo(1:st%nst, 1:st%nst))
608
609 ! calculate FO operator to check Hermiticity of lagrange multiplier matrix (lambda)
610 lambda = m_zero
611 fo = m_zero
612 call construct_lambda(namespace, hm, st, gr, lambda, rdm)
613
614 !Set up FO matrix to check maxFO
615 do ist = 1, st%nst
616 do jst = 1, ist - 1
617 fo(jst, ist) = - (lambda(jst, ist) - lambda(ist ,jst))
618 end do
619 end do
620 rdm%maxFO = maxval(abs(fo))
621
622 safe_deallocate_a(lambda)
623 safe_deallocate_a(fo)
624
625 pop_sub(calc_maxfo)
626 end subroutine calc_maxfo
627
628 ! ---------------------------------------------------------
629 subroutine calc_photon_number(space, gr, st, photons, photon_number_state, ekin_state, epot_state)
630 class(space_t), intent(in) :: space
631 type(grid_t), intent(in) :: gr
632 type(states_elec_t), intent(in) :: st
633 type(photon_mode_t), intent(inout) :: photons
634 real(real64), intent(out) :: photon_number_state(:)
635 real(real64), intent(out) :: ekin_state(:)
636 real(real64), intent(out) :: epot_state(:)
637
638 integer :: ist, dim_photon
639 real(real64) :: q2_exp, laplace_exp
640 real(real64), allocatable :: psi(:, :), psi_q2(:), dpsidq(:), d2psidq2(:)
641
642 push_sub(calc_photon_number)
643
644 ! The photon dimension is always the last
645 dim_photon = space%dim
646
647 safe_allocate(psi(1:gr%np_part, 1))
648 safe_allocate(psi_q2(1:gr%np))
649 safe_allocate(dpsidq(1:gr%np_part))
650 safe_allocate(d2psidq2(1:gr%np))
651
652 photons%number(1) = m_zero
653
654 do ist = 1, st%nst
655 call states_elec_get_state(st, gr, ist, 1, psi)
656
657 ! <phi(ist)|d^2/dq^2|phi(ist)> ~= <phi(ist)| d/dq (d/dq|phi(ist)>)
658 call dderivatives_partial(gr%der, psi(:, 1), dpsidq(:), dim_photon, ghost_update = .true., set_bc = .true.)
659 call dderivatives_partial(gr%der, dpsidq(1:gr%np_part), d2psidq2(:), dim_photon, ghost_update = .true., set_bc = .true.)
660 laplace_exp = dmf_dotp(gr, psi(:, 1), d2psidq2(:))
661 ekin_state(ist) = -m_half*laplace_exp
662
663 ! <phi(ist)|q^2|psi(ist)>= |q|psi(ist)>|^2
664 psi_q2(1:gr%np) = psi(1:gr%np, 1) * gr%x(1:gr%np, dim_photon)**2
665 q2_exp = dmf_dotp(gr, psi(:, 1), psi_q2(:))
666 epot_state(ist) = m_half * photons%omega(1)**2 * q2_exp
667
668 !! N_phot(ist)=( <phi_i|H_ph|phi_i>/omega - 0.5 ) / N_elec
669 !! with <phi_i|H_ph|phi_i>=-0.5* <phi(ist)|d^2/dq^2|phi(ist)> + 0.5*omega <phi(ist)|q^2|psi(ist)>
670 photon_number_state(ist) = -m_half*laplace_exp / photons%omega(1) + m_half * photons%omega(1) * q2_exp
671 photon_number_state(ist) = photon_number_state(ist) - m_half
672
673 !! N_phot_total= sum_ist occ_ist*N_phot(ist)
674 photons%number(1) = photons%number(1) + (photon_number_state(ist) + m_half)*st%occ(ist, 1) ! 0.5 must be added again to do the normalization due to the total charge correctly
675 end do
676
677 photons%number(1) = photons%number(1) - st%qtot/m_two
678
679 safe_deallocate_a(psi)
680 safe_deallocate_a(psi_q2)
681 safe_deallocate_a(dpsidq)
682 safe_deallocate_a(d2psidq2)
683
684 pop_sub(calc_photon_number)
685 end subroutine calc_photon_number
686
687 ! ---------------------------------------------------------
688
689 ! reset occ.num. to 2/0
690 subroutine set_occ_pinning(st)
691 type(states_elec_t), intent(inout) :: st
692
693 real(real64), allocatable :: occin(:,:)
694
695 push_sub(set_occ_pinning)
696
697 safe_allocate(occin(1:st%nst, 1:st%nik))
698
699 occin(1:st%nst, 1:st%nik) = st%occ(1:st%nst, 1:st%nik)
700 where(occin(:,:) < m_one) occin(:,:) = m_zero
701 where(occin(:,:) > m_one) occin(:,:) = st%smear%el_per_state
702
703 st%occ = occin
704
705 safe_deallocate_a(occin)
706
707 pop_sub(set_occ_pinning)
708 end subroutine set_occ_pinning
709
710
711 ! ---------------------------------------------------------
712 ! dummy routine for occupation numbers which only calculates the necessary variables for further use
713 ! used in Hartree-Fock mode
714 subroutine scf_occ_no(rdm, namespace, gr, hm, space, st, energy)
715 type(rdm_t), intent(inout) :: rdm
716 type(namespace_t), intent(in) :: namespace
717 type(grid_t), intent(in) :: gr
718 type(hamiltonian_elec_t), intent(in) :: hm
719 class(space_t), intent(in) :: space
720 type(states_elec_t), intent(inout) :: st
721 real(real64), intent(out) :: energy
723 integer :: ist
724
725 push_sub(scf_occ_no)
726
727 write(message(1),'(a)') 'SKIP Optimization of occupation numbers'
728 call messages_info(1, namespace=namespace)
729
730 call set_occ_pinning(st)
731
732 energy = m_zero
733
734 call rdm_derivatives(rdm, namespace, hm, st, gr, space)
735
736 call total_energy_rdm(rdm, st%occ(:,1), energy)
737
738 rdm%occsum = sum(st%occ(1:st%nst, 1:st%nik))
739
740 write(message(1),'(a4,5x,a12)')'#st','Occupation'
741 call messages_info(1, namespace=namespace)
742
743 do ist = 1, st%nst
744 write(message(1),'(i4,3x,f11.9)') ist, st%occ(ist, 1)
745 call messages_info(1, namespace=namespace)
746 end do
747
748 write(message(1),'(a,1x,f13.9)') 'Sum of occupation numbers', rdm%occsum
749 write(message(2),'(a,es20.10)') 'Total energy occ', units_from_atomic(units_out%energy,energy + hm%ep%eii)
750 call messages_info(2, namespace=namespace)
751
752 pop_sub(scf_occ_no)
753 end subroutine scf_occ_no
754
755 ! scf for the occupation numbers
756 subroutine scf_occ(rdm, namespace, gr, hm, space, st, energy)
757 type(rdm_t), target, intent(inout) :: rdm
758 type(namespace_t), intent(in) :: namespace
759 type(grid_t), intent(in) :: gr
760 type(hamiltonian_elec_t), intent(in) :: hm
761 class(space_t), intent(in) :: space
762 type(states_elec_t), intent(inout) :: st
763 real(real64), intent(out) :: energy
764
765 integer :: ist, icycle, ierr
766 real(real64) :: sumgi1, sumgi2, sumgim, mu1, mu2, mum, dinterv, thresh_occ
767 real(real64), allocatable :: occin(:,:)
768 real(real64), parameter :: smallocc = 0.00001_real64
769 real(real64), allocatable :: theta(:)
770 real(real64) :: objective
771
772 push_sub(scf_occ)
773 call profiling_in("SCF_OCC")
774
775 write(message(1),'(a)') 'Optimization of occupation numbers'
776 call messages_info(1, namespace=namespace)
777
778 safe_allocate(occin(1:st%nst, 1:st%nik))
779 safe_allocate(theta(1:st%nst))
780
781 occin = m_zero
782 theta = m_zero
783 energy = m_zero
784
785 ! Defines a threshold on occ nums to avoid numerical instabilities.
786 ! Needs to be changed consistently with the same variable in objective_rdmft
787 thresh_occ = 1e-14_real64
788
789 !Initialize the occin. Smallocc is used for numerical stability
790 occin(1:st%nst, 1:st%nik) = st%occ(1:st%nst, 1:st%nik)
791 where(occin(:,:) < smallocc) occin(:,:) = smallocc
792 where(occin(:,:) > st%smear%el_per_state - smallocc) occin(:,:) = st%smear%el_per_state - smallocc
793
794 !Renormalize the occupation numbers
795 rdm%occsum = st%qtot
796
797 st%occ = occin
798
799 call rdm_derivatives(rdm, namespace, hm, st, gr, space)
800
801 !finding the chemical potential mu such that the occupation numbers sum up to the number of electrons
802 !bisection to find the root of rdm%occsum-st%qtot=M_ZERO
803 mu1 = rdm%mu !initial guess for mu
804 mu2 = -1.0e-6_real64
805 dinterv = m_half
806
807 ! Set pointer to rdm, so that it is available in the functions called by the minimizer
808 rdm_ptr => rdm
809
810 !use n_j=sin^2(2pi*theta_j) to treat pinned states, minimize for both intial mu
811 theta(:) = asin(sqrt(occin(:, 1)/st%smear%el_per_state))*(m_half/m_pi)
812 call minimize_multidim(minmethod_bfgs, st%nst, theta, 0.05_real64, 0.01_real64, &
813 1e-12_real64, 1e-12_real64, 200, objective_rdmft, write_iter_info_rdmft, objective, ierr)
814 sumgi1 = rdm%occsum - st%qtot
815 rdm%mu = mu2
816 theta(:) = asin(sqrt(occin(:, 1)/st%smear%el_per_state))*(m_half/m_pi)
817 call minimize_multidim(minmethod_bfgs, st%nst, theta, 0.05_real64, 0.01_real64, &
818 1e-12_real64, 1e-12_real64, 200, objective_rdmft, write_iter_info_rdmft, objective, ierr)
819 sumgi2 = rdm%occsum - st%qtot
820
821 ! Adjust the interval between the initial mu to include the root of rdm%occsum-st%qtot=M_ZERO
822 do while (sumgi1*sumgi2 > m_zero)
823 if (sumgi2 > m_zero) then
824 mu2 = mu1
825 sumgi2 = sumgi1
826 mu1 = mu1 - dinterv
827 rdm%mu = mu1
828 theta(:) = asin(sqrt(occin(:, 1)/st%smear%el_per_state))*(m_half/m_pi)
829 call minimize_multidim(minmethod_bfgs, st%nst, theta, 0.05_real64, 0.01_real64, &
830 1e-12_real64, 1e-12_real64, 200, objective_rdmft, write_iter_info_rdmft, objective, ierr)
831 sumgi1 = rdm%occsum - st%qtot
832 else
833 mu1 = mu2
834 sumgi1 = sumgi2
835 mu2 = mu2 + dinterv
836 rdm%mu = mu2
837 theta(:) = asin(sqrt(occin(:, 1)/st%smear%el_per_state))*(m_half/m_pi)
838 call minimize_multidim(minmethod_bfgs, st%nst, theta, 0.05_real64, 0.01_real64, &
839 1e-12_real64, 1e-12_real64, 200, objective_rdmft, write_iter_info_rdmft, objective, ierr)
840 sumgi2 = rdm%occsum - st%qtot
841 end if
842 end do
843
844 do icycle = 1, 50
845 mum = (mu1 + mu2)*m_half
846 rdm%mu = mum
847 theta(:) = asin(sqrt(occin(:, 1)/st%smear%el_per_state))*(m_half/m_pi)
848 call minimize_multidim(minmethod_bfgs, st%nst, theta, 0.05_real64, 0.0001_real64, &
849 1e-12_real64, 1e-12_real64, 200, objective_rdmft, write_iter_info_rdmft, objective, ierr)
850 sumgim = rdm%occsum - st%qtot
851
852 if (sumgi1*sumgim < m_zero) then
853 mu2 = mum
854 else
855 mu1 = mum
856 sumgi1 = sumgim
857 end if
858
859 ! check occ.num. threshold again after minimization
860 do ist = 1, st%nst
861 st%occ(ist,1) = m_two*sin(theta(ist)*m_pi*m_two)**2
862 if (st%occ(ist,1) <= thresh_occ ) st%occ(ist,1) = thresh_occ
863 end do
864
865 if (abs(sumgim) < rdm%toler .or. abs((mu1-mu2)*m_half) < rdm%toler) exit
866 end do
867
868 nullify(rdm_ptr)
869
870 if (icycle >= 50) then
871 write(message(1),'(a,1x,f11.4)') 'Bisection ended without finding mu, sum of occupation numbers:', rdm%occsum
872 call messages_fatal(1, namespace=namespace)
873 end if
874
875 do ist = 1, st%nst
876 st%occ(ist, 1) = st%smear%el_per_state*sin(theta(ist)*m_pi*m_two)**2
877 end do
878
879 objective = objective + rdm%mu*(rdm%occsum - rdm%qtot)
880 energy = objective
881
882 write(message(1),'(a4,5x,a12)')'#st','Occupation'
883 call messages_info(1, namespace=namespace)
884
885 do ist = 1, st%nst
886 write(message(1),'(i4,3x,f14.12)') ist, st%occ(ist, 1)
887 call messages_info(1, namespace=namespace)
888 end do
889
890 write(message(1),'(a,3x,f11.9)') 'Sum of occupation numbers: ', rdm%occsum
891 write(message(2),'(a,11x,es20.10)') 'Total energy: ', units_from_atomic(units_out%energy, energy + hm%ep%eii)
892 call messages_info(2, namespace=namespace)
893
894 safe_deallocate_a(occin)
895 safe_deallocate_a(theta)
896
897 call profiling_out("SCF_OCC")
898 pop_sub(scf_occ)
899 end subroutine scf_occ
900
901 ! ---------------------------------------------------------
902 subroutine objective_rdmft(size, theta, objective, getgrad, df)
903 integer, intent(in) :: size
904 real(real64), intent(in) :: theta(size)
905 real(real64), intent(inout) :: objective
906 integer, intent(in) :: getgrad
907 real(real64), intent(inout) :: df(size)
908
909 integer :: ist
910 real(real64) :: thresh_occ, thresh_theta
911 real(real64), allocatable :: dE_dn(:),occ(:)
912
913 push_sub(objective_rdmft)
914
915 assert(size == rdm_ptr%nst)
916
917 safe_allocate(de_dn(1:size))
918 safe_allocate(occ(1:size))
919
920 occ = m_zero
921
922 ! Defines a threshold on occ nums to avoid numerical instabilities.
923 ! Needs to be changed consistently with the same variable in scf_occ
924 thresh_occ = 1e-14_real64
925 thresh_theta = asin(sqrt(thresh_occ/m_two))*(m_half/m_pi)
926
927 do ist = 1, size
928 occ(ist) = m_two*sin(theta(ist)*m_pi*m_two)**2
929 if (occ(ist) <= thresh_occ ) occ(ist) = thresh_occ
930 end do
931
932 rdm_ptr%occsum = sum(occ(1:size))
933
934 !calculate the total energy without nuclei interaction and the energy
935 !derivatives with respect to the occupation numbers
936
937 call total_energy_rdm(rdm_ptr, occ, objective, de_dn)
938 do ist = 1, size
939 if (occ(ist) <= thresh_occ ) then
940 df(ist) = m_four*m_pi*sin(m_four*thresh_theta*m_pi)*(de_dn(ist) - rdm_ptr%mu)
941 else
942 df(ist) = m_four*m_pi*sin(m_four*theta(ist)*m_pi)*(de_dn(ist) - rdm_ptr%mu)
943 end if
944 end do
945 objective = objective - rdm_ptr%mu*(rdm_ptr%occsum - rdm_ptr%qtot)
946
947 safe_deallocate_a(de_dn)
948 safe_deallocate_a(occ)
949
950 pop_sub(objective_rdmft)
951 end subroutine objective_rdmft
952
953 ! ---------------------------------------------------------
954 subroutine write_iter_info_rdmft(iter, size, energy, maxdr, maxdf, theta)
955 integer, intent(in) :: iter
956 integer, intent(in) :: size
957 real(real64), intent(in) :: energy, maxdr, maxdf
958 real(real64), intent(in) :: theta(size)
959
960 push_sub(write_iter_info_rdmft)
961
962 ! Nothing to do.
963
964 pop_sub(write_iter_info_rdmft)
965 end subroutine write_iter_info_rdmft
966
967 ! scf for the natural orbitals
968 subroutine scf_orb(rdm, namespace, gr, st, hm, space, energy)
969 type(rdm_t), intent(inout) :: rdm
970 type(namespace_t), intent(in) :: namespace
971 type(grid_t), intent(in) :: gr
972 type(states_elec_t), intent(inout) :: st
973 type(hamiltonian_elec_t), intent(in) :: hm
974 class(space_t), intent(in) :: space
975 real(real64), intent(out) :: energy
976
977 integer :: ist, jst
978 real(real64), allocatable :: lambda(:,:), fo(:,:)
979
980 push_sub(scf_orb)
981 call profiling_in("SCF_ORB_BASIS")
982
983 !matrix of Lagrange Multipliers from Equation (8), Piris and Ugalde, Vol. 30, No. 13, J. Comput. Chem.
984 safe_allocate(lambda(1:st%nst,1:st%nst))
985 safe_allocate(fo(1:st%nst, 1:st%nst)) !Generalized Fockian Equation (11)
986
987 lambda = m_zero
988 fo = m_zero
989
990 call construct_lambda(namespace, hm, st, gr, lambda, rdm)
991
992 !Set up fo matrix
993 if (rdm%iter==1) then
994 do ist = 1, st%nst
995 do jst = 1, ist
996 fo(ist, jst) = m_half*(lambda(ist, jst) + lambda(jst, ist))
997 fo(jst, ist) = fo(ist, jst)
998 end do
999 end do
1000 else
1001 do ist = 1, st%nst
1002 do jst = 1, ist - 1
1003 fo(jst, ist) = - ( lambda(jst, ist) - lambda(ist ,jst))
1004 end do
1005 end do
1006 rdm%maxfo = maxval(abs(fo))
1007 do ist = 1, st%nst
1008 fo(ist, ist) = rdm%evalues(ist)
1009 do jst = 1, ist-1
1010 if(abs(fo(jst, ist)) > rdm%scale_f) then
1011 fo(jst, ist) = rdm%scale_f*fo(jst,ist)/abs(fo(jst, ist))
1012 end if
1013 fo(ist, jst) = fo(jst, ist)
1014 end do
1015 end do
1016 end if
1017
1018 call lalg_eigensolve(st%nst, fo, rdm%evalues)
1019 call assign_eigfunctions(rdm, st, fo)
1020 call sum_integrals(rdm) ! to calculate rdm%Coul and rdm%Exch with the new rdm%vecnat
1021 call rdm_derivatives(rdm, namespace, hm, st, gr, space)
1022 call total_energy_rdm(rdm, st%occ(:,1), energy)
1023
1024 safe_deallocate_a(lambda)
1025 safe_deallocate_a(fo)
1026
1027 call profiling_out("SCF_ORB_BASIS")
1028 pop_sub(scf_orb)
1029 end subroutine scf_orb
1030
1031
1032 !-----------------------------------------------------------------
1033 ! Minimize the total energy wrt. an orbital by conjugate gradient
1034 !-----------------------------------------------------------------
1035 subroutine scf_orb_cg(rdm, namespace, space, gr, ions, ext_partners, st, ks, hm, energy)
1036 type(rdm_t), intent(inout) :: rdm
1037 type(namespace_t), intent(in) :: namespace
1038 type(electron_space_t), intent(in) :: space
1039 type(grid_t), intent(in) :: gr
1040 type(ions_t), intent(in) :: ions
1041 type(partner_list_t), intent(in) :: ext_partners
1042 type(states_elec_t), intent(inout) :: st
1043 type(v_ks_t), intent(inout) :: ks
1044 type(hamiltonian_elec_t), intent(inout) :: hm
1045 real(real64), intent(out) :: energy
1046
1047 integer :: ik, ist, maxiter
1048
1049
1050 push_sub(scf_orb_cg)
1051 call profiling_in("CG")
1052
1053 call v_ks_calc(ks, namespace, space, hm, st, ions, ext_partners)
1054 call hm%update(gr, namespace, space, ext_partners)
1055
1056 rdm%eigens%converged = 0
1057 if(mpi_grp_is_root(mpi_world) .and. .not. debug%info) then
1058 call loct_progress_bar(-1, st%lnst*st%d%kpt%nlocal)
1059 end if
1060 do ik = st%d%kpt%start, st%d%kpt%end
1061 rdm%eigens%matvec = 0
1062 maxiter = rdm%eigens%es_maxiter
1063 call deigensolver_cg(namespace, gr, st, hm, hm%xc, rdm%eigens%pre, rdm%eigens%tolerance, maxiter, &
1064 rdm%eigens%converged(ik), ik, rdm%eigens%diff(:, ik), rdm%eigens%energy_change_threshold, &
1065 rdm%eigens%orthogonalize_to_all, rdm%eigens%conjugate_direction, rdm%eigens%additional_terms)
1066
1067 if (.not. rdm%eigens%folded_spectrum) then
1068 ! recheck convergence after subspace diagonalization, since states may have reordered (copied from eigensolver_run)
1069 rdm%eigens%converged(ik) = 0
1070 do ist = 1, st%nst
1071 if(rdm%eigens%diff(ist, ik) < rdm%eigens%tolerance) then
1072 rdm%eigens%converged(ik) = ist
1073 else
1074 exit
1075 end if
1076 end do
1077 end if
1078 end do
1079
1080 if(mpi_grp_is_root(mpi_world) .and. .not. debug%info) then
1081 write(stdout, '(1x)')
1082 end if
1083
1084 ! calculate total energy with new states
1085 call density_calc (st, gr, st%rho)
1086 call v_ks_calc(ks, namespace, space, hm, st, ions, ext_partners)
1087 call hm%update(gr, namespace, space, ext_partners)
1088 call rdm_derivatives(rdm, namespace, hm, st, gr, space)
1089
1090 call total_energy_rdm(rdm, st%occ(:,1), energy)
1091
1092 call profiling_out("CG")
1093 pop_sub(scf_orb_cg)
1094 end subroutine scf_orb_cg
1095
1096
1097 ! ----------------------------------------
1098 ! constructs the Lagrange multiplyers needed for the orbital minimization
1099 subroutine construct_lambda(namespace, hm, st, gr, lambda, rdm)
1100 type(namespace_t), intent(in) :: namespace
1101 type(hamiltonian_elec_t), intent(in) :: hm
1102 type(states_elec_t), intent(inout) :: st
1103 type(grid_t), intent(in) :: gr
1104 real(real64), intent(out) :: lambda(:,:)
1105 type(rdm_t), intent(inout) :: rdm
1106
1107 real(real64), allocatable :: hpsi(:,:), hpsi1(:,:), dpsi(:,:), dpsi1(:,:)
1108 real(real64), allocatable :: fock(:,:,:), fvec(:)
1109 integer :: ist, iorb, jorb, jst
1110
1111 push_sub(construct_lambda)
1112
1113 lambda = m_zero
1114
1115 !calculate the Lagrange multiplyer lambda matrix on the grid, Eq. (9), Piris and Ugalde, Vol. 30, No. 13, J. Comput. Chem.
1116 if (.not. rdm%do_basis) then
1117 safe_allocate(hpsi(1:gr%np,1:st%d%dim))
1118 safe_allocate(hpsi1(1:gr%np,1:st%d%dim))
1119 safe_allocate(dpsi(1:gr%np_part ,1:st%d%dim))
1120 safe_allocate(dpsi1(1:gr%np_part ,1:st%d%dim))
1121
1122 do iorb = 1, st%nst
1123 call states_elec_get_state(st, gr, iorb, 1, dpsi)
1124 call dhamiltonian_elec_apply_single(hm, namespace, gr, dpsi, hpsi, iorb, 1)
1125
1126 do jorb = iorb, st%nst
1127 ! calculate <phi_j|H|phi_i> =lam_ji
1128 call states_elec_get_state(st, gr, jorb, 1, dpsi1)
1129 lambda(jorb, iorb) = dmf_dotp(gr, dpsi1(:,1), hpsi(:,1))
1130
1131 ! calculate <phi_i|H|phi_j>=lam_ij
1132 if (.not. iorb == jorb ) then
1133 call dhamiltonian_elec_apply_single(hm, namespace, gr, dpsi1, hpsi1, jorb, 1)
1134 lambda(iorb, jorb) = dmf_dotp(gr, dpsi(:,1), hpsi1(:,1))
1135 end if
1136 end do
1137 end do
1138
1139
1140 else ! calculate the same lambda matrix on the basis
1141 !call sum_integrals(rdm)
1142 safe_allocate(fvec(1:st%nst))
1143 safe_allocate(fock(1:st%nst, 1:st%nst, 1:st%nst))
1144 fock = m_zero
1145
1146 do iorb = 1, st%nst
1147 do ist = 1, st%nst
1148 do jst = 1, ist
1149 fock(ist, jst, iorb) = st%occ(iorb, 1)*rdm%eone_int(ist,jst)
1150 do jorb = 1, st%nst
1151 !The coefficient of the Exchange term below is only for the Mueller functional
1152 fock(ist ,jst, iorb) = fock(ist, jst, iorb) + st%occ(iorb, 1)*st%occ(jorb, 1)*rdm%Coul(ist, jst, jorb) &
1153 - sqrt(st%occ(iorb, 1))*sqrt(st%occ(jorb, 1))*rdm%Exch(ist, jst, jorb)
1154 end do
1155 fock(jst, ist, iorb) = fock(ist, jst, iorb)
1156 end do
1157 end do
1158 end do
1159
1160 do jorb = 1, st%nst
1161 do ist = 1, st%nst
1162 fvec(ist) = m_zero
1163 do jst = 1, st%nst
1164 fvec(ist) = fvec(ist) + fock(ist, jst, jorb)*rdm%vecnat(jst, jorb)
1165 end do
1166 end do
1167 do iorb= 1, st%nst
1168 lambda(iorb, jorb) = m_zero
1169 do ist = 1, st%nst
1170 lambda(iorb, jorb) = lambda(iorb, jorb) + rdm%vecnat(ist, iorb)*fvec(ist)
1171 end do
1172 end do
1173 end do
1174 end if
1175
1176
1177 if (.not. rdm%do_basis) then
1178 safe_deallocate_a(hpsi)
1179 safe_deallocate_a(hpsi1)
1180 safe_deallocate_a(dpsi)
1181 safe_deallocate_a(dpsi1)
1182 else
1183 safe_deallocate_a(fvec)
1184 safe_deallocate_a(fock)
1185 end if
1186
1187 pop_sub(construct_lambda)
1188 end subroutine construct_lambda
1189
1190 ! ----------------------------------------
1191
1192 ! finds the new states after the minimization of the orbitals (Piris method)
1193 subroutine assign_eigfunctions(rdm, st, lambda)
1194 type(rdm_t), intent(inout) :: rdm
1195 type(states_elec_t), intent(inout) :: st
1196 real(real64), intent(in) :: lambda(:, :)
1197
1198 integer :: iorb, jorb, ist
1199 real(real64), allocatable :: vecnat_new(:,:)
1200
1201 push_sub(assign_eigenfunctions)
1202
1203 safe_allocate(vecnat_new(1:st%nst, 1:st%nst))
1204 do iorb = 1, st%nst
1205 do ist = 1, st%nst
1206 vecnat_new(ist, iorb) = m_zero
1207 do jorb = 1, st%nst
1208 vecnat_new(ist , iorb) = vecnat_new(ist, iorb) + rdm%vecnat(ist, jorb)*lambda(jorb, iorb)
1209 end do
1210 end do
1211 end do
1212
1213 rdm%vecnat = vecnat_new
1214
1215 safe_deallocate_a(vecnat_new)
1216
1217 pop_sub(assign_eigenfunctions)
1218 end subroutine assign_eigfunctions
1219
1220 ! --------------------------------------------
1221
1222 ! calculates the total energy when only the occupation numbers are updated
1223 subroutine total_energy_rdm(rdm, occ, energy, dE_dn)
1224 type(rdm_t), intent(in) :: rdm
1225 real(real64), intent(in) :: occ(:)
1226 real(real64), intent(out) :: energy
1227 real(real64), optional, intent(out) :: dE_dn(:)
1228
1229 integer :: ist, jst
1230 real(real64), allocatable :: V_h(:), V_x(:)
1231
1232 push_sub(total_energy_rdm)
1233
1234 safe_allocate(v_h(1:rdm%nst))
1235 safe_allocate(v_x(1:rdm%nst))
1236
1237 energy = m_zero
1238 v_h = m_zero
1239 v_x = m_zero
1240
1241 !Calculate hartree and exchange contribution
1242 !This is only for the Mueller functional and has to be changed
1243 do ist = 1, rdm%nst
1244 do jst = 1, rdm%nst
1245 v_h(ist) = v_h(ist) + occ(jst)*rdm%hartree(ist, jst)
1246 v_x(ist) = v_x(ist) - sqrt(occ(jst))*rdm%exchange(ist, jst)
1247 end do
1248 v_x(ist) = v_x(ist)*m_half/max(sqrt(occ(ist)), 1.0e-16_real64)
1249 end do
1250
1251
1252 !Calculate the energy derivative with respect to the occupation numbers
1253 if (present(de_dn)) then
1254 de_dn(:) = rdm%eone(:) + v_h(:) + v_x(:)
1255 end if
1256
1257 !Total energy calculation without nuclei interaction
1258 do ist = 1, rdm%nst
1259 energy = energy + occ(ist)*rdm%eone(ist) &
1260 + m_half*occ(ist)*v_h(ist) &
1261 + occ(ist)*v_x(ist)
1262 end do
1263
1264 safe_deallocate_a(v_h)
1265 safe_deallocate_a(v_x)
1266
1267 pop_sub(total_energy_rdm)
1268 end subroutine total_energy_rdm
1269
1270 ! ----------------------------------------
1271 ! calculates the derivatives of the energy terms with respect to the occupation numbers
1272 subroutine rdm_derivatives(rdm, namespace, hm, st, gr, space)
1273 type(rdm_t), intent(inout) :: rdm
1274 type(namespace_t), intent(in) :: namespace
1275 type(hamiltonian_elec_t), intent(in) :: hm
1276 type(states_elec_t), intent(inout) :: st
1277 type(grid_t), intent(in) :: gr
1278 class(space_t), intent(in) :: space
1279
1280
1281 real(real64), allocatable :: hpsi(:,:), rho1(:), rho(:), dpsi(:,:), dpsi2(:,:)
1282 real(real64), allocatable :: v_ij(:,:,:,:,:)
1283 real(real64) :: dd
1284 type(states_elec_t) :: xst
1285
1286 integer :: ist, jst, nspin_, iorb, jorb
1287
1288 push_sub(rdm_derivatives)
1289
1290
1291 nspin_ = min(st%d%nspin, 2)
1292
1293 if (rdm%do_basis.eqv..false.) then
1294 safe_allocate(hpsi(1:gr%np, 1:st%d%dim))
1295 safe_allocate(rho1(1:gr%np))
1296 safe_allocate(rho(1:gr%np))
1297 safe_allocate(dpsi(1:gr%np_part, 1:st%d%dim))
1298 safe_allocate(dpsi2(1:gr%np, 1:st%d%dim))
1299 safe_allocate(v_ij(1:gr%np, 1:st%nst, 1:st%nst, 1:st%nik, 1:st%nik))
1300
1301 v_ij = m_zero
1302 rdm%eone = m_zero
1303 rdm%hartree = m_zero
1304 rdm%exchange = m_zero
1305
1306 !derivative of one-electron energy with respect to the natural orbitals occupation number
1307 do ist = 1, st%nst
1308 call states_elec_get_state(st, gr, ist, 1, dpsi)
1309
1310 ! calculate one-body energy
1311 call dhamiltonian_elec_apply_single(hm, namespace, gr, dpsi, hpsi, ist, 1, &
1313 rdm%eone(ist) = dmf_dotp(gr, dpsi(:, 1), hpsi(:, 1))
1314 end do
1315
1316 !integrals used for the hartree and exchange parts of the total energy and their derivatives
1317 ! maybe better to let that be done from the lower level routines like hamiltonian apply?
1318 !
1319 ! only used to calculate total energy
1320 call xst%nullify()
1321 call dexchange_operator_compute_potentials(hm%exxop, namespace, space, gr, st, xst, hm%kpoints, f_out = v_ij)
1322 call states_elec_end(xst)
1323
1324 do ist = 1, st%nst
1325 call states_elec_get_state(st, gr, ist, 1, dpsi)
1326
1327 rho1(1:gr%np) = dpsi(1:gr%np, 1)**2
1328
1329 do jst = ist, st%nst
1330 rdm%hartree(ist, jst) = dmf_dotp(gr, rho1, v_ij(:,jst, jst, 1, 1))
1331 rdm%hartree(jst, ist) = rdm%hartree(ist, jst)
1332 call states_elec_get_state(st, gr, jst, 1, dpsi2)
1333 rho(1:gr%np) = dpsi2(1:gr%np, 1)*dpsi(1:gr%np, 1)
1334 rdm%exchange(ist, jst) = dmf_dotp(gr, rho, v_ij(:, ist, jst, 1, 1))
1335 rdm%exchange(jst, ist) = rdm%exchange(ist, jst)
1336 end do
1337 end do
1338
1339
1340 safe_deallocate_a(hpsi)
1341 safe_deallocate_a(rho)
1342 safe_deallocate_a(rho1)
1343 safe_deallocate_a(dpsi)
1344 safe_deallocate_a(dpsi2)
1345 safe_deallocate_a(v_ij)
1346
1347 else !if energy derivatives are expanded in a basis set
1348
1349 do iorb = 1, st%nst
1350 rdm%eone(iorb) = m_zero
1351 do ist = 1, st%nst
1352 do jst = 1, st%nst
1353 dd = rdm%vecnat(ist, iorb)*rdm%vecnat(jst, iorb)
1354 rdm%eone(iorb) = rdm%eone(iorb) + dd*rdm%eone_int(ist, jst)
1355 end do
1356 end do
1357 end do
1358
1359 do iorb = 1, st%nst
1360 do jorb =1 , iorb
1361 rdm%hartree(iorb ,jorb) = m_zero
1362 rdm%exchange(iorb,jorb) = m_zero
1363 do ist =1, st%nst
1364 do jst =1, st%nst
1365 dd = rdm%vecnat(ist, iorb)*rdm%vecnat(jst, iorb)
1366 rdm%hartree(iorb ,jorb) = rdm%hartree(iorb ,jorb)+rdm%Coul(ist,jst, jorb)*dd
1367 rdm%exchange(iorb ,jorb) = rdm%exchange(iorb ,jorb)+rdm%Exch(ist,jst, jorb)*dd
1368 end do
1369 end do
1370 rdm%hartree(jorb, iorb) = rdm%hartree(iorb, jorb)
1371 rdm%exchange(jorb, iorb) = rdm%exchange(iorb, jorb)
1372 end do
1373 end do
1374 end if
1375
1376 pop_sub(rdm_derivatives)
1377 end subroutine rdm_derivatives
1378
1379 ! --------------------------------------------
1380 !calculates the one electron integrals in the basis of the initial orbitals
1381 subroutine rdm_integrals(rdm, namespace, hm, st, mesh)
1382 type(rdm_t), intent(inout) :: rdm
1383 type(namespace_t), intent(in) :: namespace
1384 type(hamiltonian_elec_t), intent(in) :: hm
1385 type(states_elec_t), intent(in) :: st
1386 class(mesh_t), intent(in) :: mesh
1387
1388 real(real64), allocatable :: hpsi(:,:)
1389 real(real64), allocatable :: dpsi(:,:), dpsi2(:,:)
1390 integer :: ist, jst
1391
1392 push_sub(rdm_integrals)
1393
1394 safe_allocate(dpsi(1:mesh%np_part, 1:st%d%dim))
1395 safe_allocate(dpsi2(1:mesh%np, 1:st%d%dim))
1396 safe_allocate(hpsi(1:mesh%np, 1:st%d%dim))
1397
1398 !calculate integrals of the one-electron energy term with respect to the initial orbital basis
1399 do ist = 1, st%nst
1400 call states_elec_get_state(st, mesh, ist, 1, dpsi)
1401 do jst = ist, st%nst
1402 call states_elec_get_state(st, mesh, jst, 1, dpsi2)
1403
1404 ! calculate one-body integrals
1405 call dhamiltonian_elec_apply_single(hm, namespace, mesh, dpsi, hpsi, ist, 1, &
1407 rdm%eone_int(jst, ist) = dmf_dotp(mesh, dpsi2(:, 1), hpsi(:, 1))
1408 rdm%eone_int(ist, jst) = rdm%eone_int(jst, ist)
1409 end do
1410 end do
1411
1412 safe_deallocate_a(hpsi)
1413 safe_deallocate_a(dpsi)
1414 safe_deallocate_a(dpsi2)
1415
1416 pop_sub(rdm_integrals)
1417 end subroutine rdm_integrals
1418
1419 ! --------------------------------------------
1420 ! constructs the Hartree and Exchange part of the RDMFT Fock matrix
1421 subroutine sum_integrals(rdm)
1422 type(rdm_t), intent(inout) :: rdm
1423
1424 integer :: ist, jst, kst, lst, iorb, icount
1425 logical :: inv_pairs
1426 real(real64) :: two_int, wij, wik, wil, wjk, wjl, wkl
1427 real(real64), allocatable :: dm(:,:,:)
1428
1429 push_sub(sum_integrals)
1430
1431 safe_allocate(dm(1:rdm%nst, 1:rdm%nst, 1:rdm%nst))
1432
1433 rdm%Coul = m_zero
1434 rdm%Exch = m_zero
1435 dm = m_zero
1436
1437 do iorb = 1, rdm%nst
1438 do ist = 1, rdm%nst
1439 do jst = 1, ist
1440 dm(ist, jst, iorb) = rdm%vecnat(ist, iorb)*rdm%vecnat(jst, iorb)
1441 dm(jst, ist, iorb) = dm(ist, jst, iorb)
1442 end do
1443 end do
1444 end do
1445
1446 do icount = 1, rdm%n_twoint
1447
1448 ist = rdm%i_index(1,icount)
1449 jst = rdm%j_index(1,icount)
1450 kst = rdm%k_index(1,icount)
1451 lst = rdm%l_index(1,icount)
1452
1453 two_int = rdm%twoint(icount)
1454
1455 ! create weights of unique integrals
1456 if(ist == jst) then
1457 wij = m_one
1458 else
1459 wij = m_two
1460 endif
1461 if(kst == lst) then
1462 wkl = m_one
1463 else
1464 wkl = m_two
1465 endif
1466
1467 if(ist == kst .and. jst /= lst) then
1468 wik = m_two
1469 else
1470 wik = m_one
1471 endif
1472 if(ist == lst .and. jst /= kst) then
1473 wil = m_two
1474 else
1475 wil = m_one
1476 endif
1477 if(jst == kst .and. ist /= lst) then
1478 wjk = m_two
1479 else
1480 wjk = m_one
1481 endif
1482 if(jst == lst .and. ist /= kst) then
1483 wjl = m_two
1484 else
1485 wjl = m_one
1486 endif
1487
1488 inv_pairs = (ist /= kst .or. jst /= lst)
1489
1490 do iorb = 1, rdm%nst
1491
1492 !the Hartree terms
1493 rdm%Coul(ist, jst, iorb) = rdm%Coul(ist, jst, iorb) + dm(kst, lst, iorb)*wkl*two_int
1494 if (inv_pairs) rdm%Coul(kst, lst, iorb) = rdm%Coul(kst, lst, iorb) + dm(ist, jst, iorb)*wij*two_int
1495
1496 !the exchange terms
1497 !weights are only included if they can differ from one
1498 rdm%Exch(ist, kst, iorb) = rdm%Exch(ist, kst, iorb) + two_int*dm(jst, lst, iorb)*wik
1499 if (kst /= lst) then
1500 rdm%Exch(ist, lst, iorb) = rdm%Exch(ist, lst, iorb) + two_int*dm(jst, kst, iorb)*wil
1501 end if
1502 if (ist /= jst) then
1503 if(jst >= kst) then
1504 rdm%Exch(jst, kst, iorb) = rdm%Exch(jst, kst, iorb) + two_int*dm(ist, lst, iorb)*wjk
1505 else
1506 if (inv_pairs) rdm%Exch(kst, jst, iorb) = rdm%Exch(kst, jst, iorb) + two_int*dm(ist, lst, iorb)
1507 end if
1508 end if
1509 if (ist /=jst .and. kst /= lst) then
1510 if (jst >= lst) then
1511 rdm%Exch(jst, lst, iorb) = rdm%Exch(jst, lst, iorb) + two_int*dm(ist, kst, iorb)*wjl
1512 else
1513 if (inv_pairs) rdm%Exch(lst, jst, iorb) = rdm%Exch(lst, jst, iorb) + two_int*dm(ist, kst, iorb)
1514 end if
1515 end if
1516
1517 end do !iorb
1518 end do !icount
1519
1520 do iorb =1, rdm%nst
1521 do ist = 1, rdm%nst
1522 do jst = 1, ist-1
1523 rdm%Coul(jst, ist, iorb) = rdm%Coul(ist, jst, iorb)
1524 rdm%Exch(jst, ist, iorb) = rdm%Exch(ist, jst, iorb)
1525 end do
1526 end do
1527 end do
1528
1529 safe_deallocate_a(dm)
1530
1531 pop_sub(sum_integrals)
1532 end subroutine sum_integrals
1533
1534end module rdmft_oct_m
1535
1536
1537!! Local Variables:
1538!! mode: f90
1539!! coding: utf-8
1540!! End:
Prints out to iunit a message in the form: ["InputVariable" = value] where "InputVariable" is given b...
Definition: messages.F90:180
double sin(double __x) __attribute__((__nothrow__
double asin(double __x) __attribute__((__nothrow__
subroutine minimize_multidim(method, dim, x, step, line_tol, tolgrad, toldr, maxiter, f, write_iter_info, minimum, ierr)
Definition: minimizer.F90:403
type(debug_t), save, public debug
Definition: debug.F90:154
This module implements a calculator for the density and defines related functions.
Definition: density.F90:120
subroutine, public density_calc(st, gr, density, istin)
Computes the density from the orbitals in st.
Definition: density.F90:608
This module calculates the derivatives (gradients, Laplacians, etc.) of a function.
subroutine, public dderivatives_partial(der, ff, op_ff, dir, ghost_update, set_bc)
apply the partial derivative along dir to a mesh function
subroutine, public deigensolver_cg(namespace, mesh, st, hm, xc, pre, tol, niter, converged, ik, diff, energy_change_threshold, orthogonalize_to_all, conjugate_direction, additional_terms, shift)
conjugate-gradients method.
Definition: eigen_cg.F90:196
subroutine, public eigensolver_init(eigens, namespace, gr, st, mc, space)
subroutine, public eigensolver_end(eigens)
subroutine, public dexchange_operator_compute_potentials(this, namespace, space, gr, st, xst, kpoints, ex, F_out)
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_four
Definition: global.F90:191
real(real64), parameter, public m_pi
some mathematical constants
Definition: global.F90:185
character(len= *), parameter, public static_dir
Definition: global.F90:251
real(real64), parameter, public m_half
Definition: global.F90:193
real(real64), parameter, public m_one
Definition: global.F90:188
This module implements the underlying real-space grid.
Definition: grid.F90:117
subroutine, public grid_write_info(gr, iunit, namespace)
Definition: grid.F90:522
integer, parameter, public term_local_external
integer, parameter, public term_non_local_potential
integer, parameter, public term_kinetic
subroutine, public dhamiltonian_elec_apply_single(hm, namespace, mesh, psi, hpsi, ist, ik, terms, set_bc, set_phase)
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 is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:115
This module defines various routines, operating on mesh functions.
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:118
subroutine, public messages_print_with_emphasis(msg, iunit, namespace)
Definition: messages.F90:930
subroutine, public messages_not_implemented(feature, namespace)
Definition: messages.F90:1125
character(len=512), private msg
Definition: messages.F90:165
subroutine, public messages_warning(no_lines, all_nodes, namespace)
Definition: messages.F90:543
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_info(no_lines, iunit, debug_only, stress, all_nodes, namespace)
Definition: messages.F90:624
integer minmethod_bfgs
Definition: minimizer.F90:134
This module contains some common usage patterns of MPI routines.
Definition: mpi_lib.F90:115
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
This module handles the communicators for the various parallelization strategies.
Definition: multicomm.F90:145
this module contains the output system
Definition: output_low.F90:115
subroutine, public output_modelmb(outp, namespace, space, dir, gr, ions, iter, st)
this module contains the output system
Definition: output.F90:115
subroutine, public output_all(outp, namespace, space, dir, gr, ions, iter, st, hm, ks)
Definition: output.F90:491
subroutine, public photon_mode_write_info(this, iunit, namespace)
subroutine, public profiling_out(label)
Increment out counter and sum up difference between entry and exit time.
Definition: profiling.F90:623
subroutine, public profiling_in(label, exclude)
Increment in counter and save entry time.
Definition: profiling.F90:552
subroutine scf_occ(rdm, namespace, gr, hm, space, st, energy)
Definition: rdmft.F90:850
subroutine calc_maxfo(namespace, hm, st, gr, rdm)
Definition: rdmft.F90:688
subroutine objective_rdmft(size, theta, objective, getgrad, df)
Definition: rdmft.F90:996
subroutine scf_orb_cg(rdm, namespace, space, gr, ions, ext_partners, st, ks, hm, energy)
Definition: rdmft.F90:1129
subroutine, public rdmft_end(rdm)
Definition: rdmft.F90:351
subroutine, public rdmft_init(rdm, namespace, gr, st, mc, space, fromScratch)
Definition: rdmft.F90:214
subroutine write_iter_info_rdmft(iter, size, energy, maxdr, maxdf, theta)
Definition: rdmft.F90:1048
subroutine set_occ_pinning(st)
Definition: rdmft.F90:784
subroutine calc_photon_number(space, gr, st, photons, photon_number_state, ekin_state, epot_state)
Definition: rdmft.F90:723
subroutine assign_eigfunctions(rdm, st, lambda)
Definition: rdmft.F90:1287
subroutine, public scf_rdmft(rdm, namespace, space, gr, ions, ext_partners, st, ks, hm, outp, restart_dump)
Definition: rdmft.F90:381
subroutine construct_lambda(namespace, hm, st, gr, lambda, rdm)
Definition: rdmft.F90:1193
subroutine sum_integrals(rdm)
Definition: rdmft.F90:1515
subroutine rdm_integrals(rdm, namespace, hm, st, mesh)
Definition: rdmft.F90:1475
subroutine scf_orb(rdm, namespace, gr, st, hm, space, energy)
Definition: rdmft.F90:1062
subroutine total_energy_rdm(rdm, occ, energy, dE_dn)
Definition: rdmft.F90:1317
subroutine scf_occ_no(rdm, namespace, gr, hm, space, st, energy)
Definition: rdmft.F90:808
subroutine rdm_derivatives(rdm, namespace, hm, st, gr, space)
Definition: rdmft.F90:1366
pure logical function, public states_are_complex(st)
subroutine, public states_elec_end(st)
finalize the states_elec_t object
subroutine, public states_elec_copy(stout, stin, exclude_wfns, exclude_eigenval, special)
make a (selective) copy of a states_elec_t object
This module handles reading and writing restart information for the states_elec_t.
subroutine, public states_elec_dump(restart, space, st, mesh, kpoints, ierr, iter, lr, st_start_writing, verbose)
brief This module defines the class unit_t which is used by the unit_systems_oct_m module.
Definition: unit.F90:132
character(len=20) pure function, public units_abbrev(this)
Definition: unit.F90:223
This module defines the unit system, used for input and output.
type(unit_system_t), public units_out
subroutine, public v_ks_write_info(ks, iunit, namespace)
Definition: v_ks.F90:644
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 scf_write_static(dir, fname)
Definition: rdmft.F90:586
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
output handler class
Definition: output_low.F90:163
The states_elec_t class contains all electronic wave functions.
int true(void)