Octopus
scf.F90
Go to the documentation of this file.
1!! Copyright (C) 2002-2014 M. Marques, A. Castro, A. Rubio, G. Bertsch, M. Oliveira
2!!
3!! This program is free software; you can redistribute it and/or modify
4!! it under the terms of the GNU General Public License as published by
5!! the Free Software Foundation; either version 2, or (at your option)
6!! any later version.
7!!
8!! This program is distributed in the hope that it will be useful,
9!! but WITHOUT ANY WARRANTY; without even the implied warranty of
10!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11!! GNU General Public License for more details.
12!!
13!! You should have received a copy of the GNU General Public License
14!! along with this program; if not, write to the Free Software
15!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16!! 02110-1301, USA.
17!!
18
19#include "global.h"
20
21module scf_oct_m
23 use berry_oct_m
26 use debug_oct_m
34 use forces_oct_m
35 use global_oct_m
36 use grid_oct_m
39 use io_oct_m
40 use ions_oct_m
41 use, intrinsic :: iso_fortran_env
43 use lcao_oct_m
44 use lda_u_oct_m
48 use loct_oct_m
50 use math_oct_m
51 use mesh_oct_m
54 use mix_oct_m
56 use mpi_oct_m
59 use output_oct_m
62 use parser_oct_m
66 use smear_oct_m
67 use space_oct_m
73 use stress_oct_m
75 use types_oct_m
76 use unit_oct_m
78 use utils_oct_m
79 use v_ks_oct_m
81 use vdw_ts_oct_m
85 use xc_oct_m
86 use xc_f03_lib_m
89 use xc_oep_oct_m
91
92 implicit none
93
94 private
95 public :: &
96 scf_t, &
97 scf_init, &
99 scf_run, &
100 scf_end, &
103
104 integer, public, parameter :: &
105 VERB_NO = 0, &
106 verb_compact = 1, &
107 verb_full = 3
108
110 type scf_t
111 private
112 integer, public :: max_iter
113
114 real(real64), public :: lmm_r
115
116 ! several convergence criteria
117 logical :: conv_eigen_error
118 logical :: check_conv
119
120 integer :: mix_field
121 logical :: lcao_restricted
122 logical :: calc_force
123 logical, public :: calc_stress
124 logical :: calc_dipole
125 logical :: calc_partial_charges
126 type(mix_t) :: smix
127 type(mixfield_t), pointer :: mixfield
128 type(eigensolver_t) :: eigens
129 integer :: mixdim1
130 logical :: forced_finish
131 type(lda_u_mixer_t) :: lda_u_mix
132 type(vtau_mixer_t) :: vtau_mix
133 type(berry_t) :: berry
134 integer :: matvec
135
136 type(criterion_list_t), public :: criterion_list
137 real(real64) :: energy_in, energy_diff, abs_dens_diff, evsum_in, evsum_out, evsum_diff
138 end type scf_t
139
140contains
141
142 ! ---------------------------------------------------------
143 subroutine scf_init(scf, namespace, gr, ions, st, mc, hm, space)
144 type(scf_t), intent(inout) :: scf
145 type(grid_t), intent(in) :: gr
146 type(namespace_t), intent(in) :: namespace
147 type(ions_t), intent(in) :: ions
148 type(states_elec_t), intent(in) :: st
149 type(multicomm_t), intent(in) :: mc
150 type(hamiltonian_elec_t), intent(inout) :: hm
151 class(space_t), intent(in) :: space
152
153 real(real64) :: rmin
154 integer :: mixdefault
155 type(type_t) :: mix_type
156 class(convergence_criterion_t), pointer :: crit
157 type(criterion_iterator_t) :: iter
158
159 push_sub(scf_init)
160
161 !%Variable MaximumIter
162 !%Type integer
163 !%Default 200
164 !%Section SCF::Convergence
165 !%Description
166 !% Maximum number of SCF iterations. The code will stop even if convergence
167 !% has not been achieved. -1 means unlimited.
168 !% 0 means just do LCAO (or read from restart), compute the eigenvalues and energy,
169 !% and stop, without updating the wavefunctions or density.
170 !%
171 !% If convergence criteria are set, the SCF loop will only stop once the criteria
172 !% are fulfilled for two consecutive iterations.
173 !%
174 !% Note that this variable is also used in the section Calculation Modes::Unoccupied States,
175 !% where it denotes the maximum number of calls of the eigensolver. In this context, the
176 !% default value is 50.
177 !%End
178 call parse_variable(namespace, 'MaximumIter', 200, scf%max_iter)
179
180 if (allocated(hm%vberry)) then
181 call berry_init(scf%berry, namespace)
182 end if
183
184 !Create the list of convergence criteria
185 call criteria_factory_init(scf%criterion_list, namespace, scf%check_conv)
186 !Setting the pointers
187 call iter%start(scf%criterion_list)
188 do while (iter%has_next())
189 crit => iter%get_next()
190 select type (crit)
191 type is (energy_criterion_t)
192 call crit%set_pointers(scf%energy_diff, scf%energy_in)
193 type is (density_criterion_t)
194 call crit%set_pointers(scf%abs_dens_diff, st%qtot)
195 type is (eigenval_criterion_t)
196 call crit%set_pointers(scf%evsum_diff, scf%evsum_out)
197 class default
198 assert(.false.)
199 end select
200 end do
201
202
203 if(.not. scf%check_conv .and. scf%max_iter < 0) then
204 call messages_write("All convergence criteria are disabled. Octopus is cowardly refusing")
206 call messages_write("to enter an infinite loop.")
208 call messages_new_line()
209 call messages_write("Please set one of the following variables to a positive value:")
212 call messages_write(" | MaximumIter | ConvEnergy | ConvAbsDens | ConvRelDens |")
214 call messages_write(" | ConvAbsEv | ConvRelEv |")
216 call messages_fatal(namespace=namespace)
217 end if
219 !%Variable ConvEigenError
220 !%Type logical
221 !%Default false
222 !%Section SCF::Convergence
223 !%Description
224 !% If true, the calculation will not be considered converged unless all states have
225 !% individual errors less than <tt>EigensolverTolerance</tt>.
226 !%
227 !% If this criterion is used, the SCF loop will only stop once it is
228 !% fulfilled for two consecutive iterations.
229 !%End
230 call parse_variable(namespace, 'ConvEigenError', .false., scf%conv_eigen_error)
231
232 if(scf%max_iter < 0) scf%max_iter = huge(scf%max_iter)
233
234 call messages_obsolete_variable(namespace, 'What2Mix', 'MixField')
235
236 ! now the eigensolver stuff
237 call eigensolver_init(scf%eigens, namespace, gr, st, mc, space)
238
239 if(scf%eigens%es_type /= rs_evo) then
240 !%Variable MixField
241 !%Type integer
242 !%Section SCF::Mixing
243 !%Description
244 !% Selects what should be mixed during the SCF cycle. Note that
245 !% currently the exact-exchange part of hybrid functionals is not
246 !% mixed at all, which would require wavefunction-mixing, not yet
247 !% implemented. This may lead to instabilities in the SCF cycle,
248 !% so starting from a converged LDA/GGA calculation is recommended
249 !% for hybrid functionals. The default depends on the <tt>TheoryLevel</tt>
250 !% and the exchange-correlation potential used.
251 !% This is not used in case of imaginary-time evolution.
252 !%Option none 0
253 !% No mixing is done. This is the default for independent
254 !% particles.
255 !%Option potential 1
256 !% The Kohn-Sham potential is mixed. This is the default for other cases.
257 !%Option density 2
258 !% Mix the density.
259 !%Option states 3
260 !% (Experimental) Mix the states. In this case, the mixing is always linear.
261 !%End
262
263 mixdefault = option__mixfield__potential
264 if(hm%theory_level == independent_particles) mixdefault = option__mixfield__none
265
266 call parse_variable(namespace, 'MixField', mixdefault, scf%mix_field)
267 if(.not.varinfo_valid_option('MixField', scf%mix_field)) call messages_input_error(namespace, 'MixField')
268 call messages_print_var_option('MixField', scf%mix_field, "what to mix during SCF cycles", namespace=namespace)
269
270 if (scf%mix_field == option__mixfield__potential .and. hm%theory_level == independent_particles) then
271 call messages_write('Input: Cannot mix the potential for non-interacting particles.')
272 call messages_fatal(namespace=namespace)
273 end if
274
275 if (scf%mix_field == option__mixfield__potential .and. hm%pcm%run_pcm) then
276 call messages_write('Input: You have selected to mix the potential.', new_line = .true.)
277 call messages_write(' This might produce convergence problems for solvated systems.', new_line = .true.)
278 call messages_write(' Mix the Density instead.')
279 call messages_warning(namespace=namespace)
280 end if
281
282 if(scf%mix_field == option__mixfield__density &
283 .and. bitand(hm%xc%family, xc_family_oep + xc_family_mgga + xc_family_hyb_mgga + xc_family_nc_mgga) /= 0) then
284
285 call messages_write('Input: You have selected to mix the density with OEP or MGGA XC functionals.', new_line = .true.)
286 call messages_write(' This might produce convergence problems. Mix the potential instead.')
287 call messages_warning(namespace=namespace)
288 end if
289
290 if(scf%mix_field == option__mixfield__states) then
291 call messages_experimental('MixField = states', namespace=namespace)
292 end if
293
294 ! Handle mixing now...
295 select case(scf%mix_field)
296 case (option__mixfield__potential, option__mixfield__density)
297 scf%mixdim1 = gr%np
298 case(option__mixfield__states)
299 ! we do not really need the mixer, except for the value of the mixing coefficient
300 scf%mixdim1 = 1
301 end select
302
303 mix_type = type_float
304
305 if (scf%mix_field /= option__mixfield__none) then
306 call mix_init(scf%smix, namespace, space, gr%der, scf%mixdim1, st%d%nspin, func_type_ = mix_type)
307 end if
308
309 ! If we use DFT+U, we also have do mix it
310 if (scf%mix_field /= option__mixfield__states .and. scf%mix_field /= option__mixfield__none ) then
311 call lda_u_mixer_init(hm%lda_u, scf%lda_u_mix, st)
312 call lda_u_mixer_init_auxmixer(hm%lda_u, namespace, scf%lda_u_mix, scf%smix, st)
313 end if
314
315 ! If we use tau-dependent MGGA, we need to mix vtau
316 if(scf%mix_field == option__mixfield__potential) then
317 call vtau_mixer_init_auxmixer(namespace, scf%vtau_mix, scf%smix, hm, gr%np, st%d%nspin)
318 end if
319
320 call mix_get_field(scf%smix, scf%mixfield)
321 else
322 scf%mix_field = option__mixfield__none
323 end if
324
325 !%Variable SCFinLCAO
326 !%Type logical
327 !%Default no
328 !%Section SCF
329 !%Description
330 !% Performs the SCF cycle with the calculation restricted to the LCAO subspace.
331 !% This may be useful for systems with convergence problems (first do a
332 !% calculation within the LCAO subspace, then restart from that point for
333 !% an unrestricted calculation).
334 !%End
335 call parse_variable(namespace, 'SCFinLCAO', .false., scf%lcao_restricted)
336 if(scf%lcao_restricted) then
337 call messages_experimental('SCFinLCAO', namespace=namespace)
338 message(1) = 'Info: SCF restricted to LCAO subspace.'
339 call messages_info(1, namespace=namespace)
340
341 if(scf%conv_eigen_error) then
342 message(1) = "ConvEigenError cannot be used with SCFinLCAO, since error is unknown."
343 call messages_fatal(1, namespace=namespace)
344 end if
345 end if
346
347
348 !%Variable SCFCalculateForces
349 !%Type logical
350 !%Section SCF
351 !%Description
352 !% This variable controls whether the forces on the ions are
353 !% calculated at the end of a self-consistent iteration. The
354 !% default is yes, unless the system only has user-defined
355 !% species.
356 !%End
357 call parse_variable(namespace, 'SCFCalculateForces', .not. ions%only_user_def, scf%calc_force)
358
359 if(scf%calc_force .and. gr%der%boundaries%spiralBC) then
360 message(1) = 'Forces cannot be calculated when using spiral boundary conditions.'
361 write(message(2),'(a)') 'Please use SCFCalculateForces = no.'
362 call messages_fatal(2, namespace=namespace)
363 end if
364 if(scf%calc_force) then
365 if (allocated(hm%ep%b_field) .or. allocated(hm%ep%a_static)) then
366 write(message(1),'(a)') 'The forces are currently not properly calculated if static'
367 write(message(2),'(a)') 'magnetic fields or static vector potentials are present.'
368 write(message(3),'(a)') 'Please use SCFCalculateForces = no.'
369 call messages_fatal(3, namespace=namespace)
370 end if
371 end if
372
373 !%Variable SCFCalculateStress
374 !%Type logical
375 !%Section SCF
376 !%Description
377 !% This variable controls whether the stress on the lattice is
378 !% calculated at the end of a self-consistent iteration. The
379 !% default is no.
380 !%End
381 call parse_variable(namespace, 'SCFCalculateStress', .false. , scf%calc_stress)
382
383 !%Variable SCFCalculateDipole
384 !%Type logical
385 !%Section SCF
386 !%Description
387 !% This variable controls whether the dipole is calculated at the
388 !% end of a self-consistent iteration. For finite systems the
389 !% default is yes. For periodic systems the default is no, unless
390 !% an electric field is being applied in a periodic direction.
391 !% The single-point Berry`s phase approximation is used for
392 !% periodic directions. Ref:
393 !% E Yaschenko, L Fu, L Resca, and R Resta, <i>Phys. Rev. B</i> <b>58</b>, 1222-1229 (1998).
394 !%End
395 call parse_variable(namespace, 'SCFCalculateDipole', .not. space%is_periodic(), scf%calc_dipole)
396 if (allocated(hm%vberry)) scf%calc_dipole = .true.
397
398 !%Variable SCFCalculatePartialCharges
399 !%Type logical
400 !%Default no
401 !%Section SCF
402 !%Description
403 !% (Experimental) This variable controls whether partial charges
404 !% are calculated at the end of a self-consistent iteration.
405 !%End
406 call parse_variable(namespace, 'SCFCalculatePartialCharges', .false., scf%calc_partial_charges)
407 if (scf%calc_partial_charges) call messages_experimental('SCFCalculatePartialCharges', namespace=namespace)
408
409 rmin = ions%min_distance()
410
411 !%Variable LocalMagneticMomentsSphereRadius
412 !%Type float
413 !%Section Output
414 !%Description
415 !% The local magnetic moments are calculated by integrating the
416 !% magnetization density in spheres centered around each atom.
417 !% This variable controls the radius of the spheres.
418 !% The default is half the minimum distance between two atoms
419 !% in the input coordinates, or 100 a.u. if there is only one atom (for isolated systems).
420 !%End
421 call parse_variable(namespace, 'LocalMagneticMomentsSphereRadius', min(m_half*rmin, lmm_r_single_atom), scf%lmm_r, &
422 unit=units_inp%length)
423 ! this variable is also used in td/td_write.F90
424
425 scf%forced_finish = .false.
426
427 pop_sub(scf_init)
428 end subroutine scf_init
429
430
431 ! ---------------------------------------------------------
432 subroutine scf_end(scf)
433 type(scf_t), intent(inout) :: scf
434
435 class(convergence_criterion_t), pointer :: crit
436 type(criterion_iterator_t) :: iter
437
438 push_sub(scf_end)
439
440 call eigensolver_end(scf%eigens)
441
442 if(scf%mix_field /= option__mixfield__none) call mix_end(scf%smix)
443
444 nullify(scf%mixfield)
445
446 if (scf%mix_field /= option__mixfield__states) then
447 call lda_u_mixer_end(scf%lda_u_mix, scf%smix)
448 call vtau_mixer_end(scf%vtau_mix, scf%smix)
449 end if
450
451 call iter%start(scf%criterion_list)
452 do while (iter%has_next())
453 crit => iter%get_next()
454 safe_deallocate_p(crit)
455 end do
456
457 pop_sub(scf_end)
458 end subroutine scf_end
459
460
461 ! ---------------------------------------------------------
462 subroutine scf_mix_clear(scf)
463 type(scf_t), intent(inout) :: scf
464
465 push_sub(scf_mix_clear)
466
467 call mix_clear(scf%smix)
468
469 if (scf%mix_field /= option__mixfield__states) then
470 call lda_u_mixer_clear(scf%lda_u_mix, scf%smix)
471 call vtau_mixer_clear(scf%vtau_mix, scf%smix)
472 end if
473
474 pop_sub(scf_mix_clear)
475 end subroutine scf_mix_clear
476
477
478 ! ---------------------------------------------------------
479 subroutine scf_run(scf, namespace, space, mc, gr, ions, ext_partners, st, ks, hm, outp, &
480 verbosity, iters_done, restart_load, restart_dump)
481 type(scf_t), intent(inout) :: scf
482 type(namespace_t), intent(in) :: namespace
483 type(electron_space_t), intent(in) :: space
484 type(multicomm_t), intent(in) :: mc
485 type(grid_t), intent(inout) :: gr
486 type(ions_t), intent(inout) :: ions
487 type(partner_list_t), intent(in) :: ext_partners
488 type(states_elec_t), intent(inout) :: st
489 type(v_ks_t), intent(inout) :: ks
490 type(hamiltonian_elec_t), intent(inout) :: hm
491 type(output_t), optional, intent(in) :: outp
492 integer, optional, intent(in) :: verbosity
493 integer, optional, intent(out) :: iters_done
494 type(restart_t), optional, intent(in) :: restart_load
495 type(restart_t), optional, intent(in) :: restart_dump
496
497 logical :: finish, converged_current, converged_last
498 integer :: iter, is, nspin, ierr, verbosity_, ib, iqn
499 integer(int64) :: what_i
500 real(real64) :: etime, itime
501 character(len=MAX_PATH_LEN) :: dirname
502 type(lcao_t) :: lcao
503 real(real64), allocatable :: rhoout(:,:), rhoin(:,:)
504 real(real64), allocatable :: vhxc_old(:,:)
505 class(wfs_elec_t), allocatable :: psioutb(:, :)
506 class(convergence_criterion_t), pointer :: crit
507 type(criterion_iterator_t) :: iterator
508 logical :: is_crit_conv, output_forces, calc_current, output_during_scf
509
510 push_sub(scf_run)
511
512 if(scf%forced_finish) then
513 message(1) = "Previous clean stop, not doing SCF and quitting."
514 call messages_fatal(1, only_root_writes = .true., namespace=namespace)
515 end if
516
517 verbosity_ = verb_full
518 if(present(verbosity)) verbosity_ = verbosity
519
520 output_during_scf = .false.
521 output_forces = .false.
522 calc_current = .false.
523
524 if (present(outp)) then
525 ! if the user has activated output=stress but not SCFCalculateStress,
526 ! we assume that is implied
527 if (outp%what(option__output__stress)) then
528 scf%calc_stress = .true.
529 end if
530
531 output_during_scf = outp%duringscf
532 calc_current = output_needs_current(outp, states_are_real(st))
533
534 if (outp%duringscf .and. outp%what(option__output__forces)) then
535 output_forces = .true.
536 end if
537 end if
538
539 if(scf%lcao_restricted) then
540 call lcao_init(lcao, namespace, space, gr, ions, st)
541 if(.not. lcao_is_available(lcao)) then
542 message(1) = 'LCAO is not available. Cannot do SCF in LCAO.'
543 call messages_fatal(1, namespace=namespace)
544 end if
545 end if
546
547 nspin = st%d%nspin
548
549 if (present(restart_load)) then
550 if (restart_has_flag(restart_load, restart_flag_rho)) then
551 ! Load density and used it to recalculated the KS potential.
552 call states_elec_load_rho(restart_load, space, st, gr, ierr)
553 if (ierr /= 0) then
554 message(1) = 'Unable to read density. Density will be calculated from states.'
555 call messages_warning(1, namespace=namespace)
556 else
557 if (bitand(ks%xc_family, xc_family_oep) == 0) then
558 call v_ks_calc(ks, namespace, space, hm, st, ions, ext_partners)
559 else
560 if (.not. restart_has_flag(restart_load, restart_flag_vhxc) .and. ks%oep%level /= oep_level_full) then
561 call v_ks_calc(ks, namespace, space, hm, st, ions, ext_partners)
562 end if
563 end if
564 end if
565 end if
566
567 if (restart_has_flag(restart_load, restart_flag_vhxc)) then
568 call hamiltonian_elec_load_vhxc(restart_load, hm, space, gr, ierr)
569 if (ierr /= 0) then
570 message(1) = 'Unable to read Vhxc. Vhxc will be calculated from states.'
571 call messages_warning(1, namespace=namespace)
572 else
573 call hm%update(gr, namespace, space, ext_partners)
574 if (bitand(ks%xc_family, xc_family_oep) /= 0) then
575 if (ks%oep%level == oep_level_full) then
576 do is = 1, st%d%nspin
577 ks%oep%vxc(1:gr%np, is) = hm%vhxc(1:gr%np, is) - hm%vhartree(1:gr%np)
578 end do
579 call v_ks_calc(ks, namespace, space, hm, st, ions, ext_partners)
580 end if
581 end if
582 end if
583 end if
584
585 if (restart_has_flag(restart_load, restart_flag_mix)) then
586 if (scf%mix_field == option__mixfield__density .or. scf%mix_field == option__mixfield__potential) then
587 call mix_load(namespace, restart_load, scf%smix, space, gr, ierr)
588 end if
589 if (ierr /= 0) then
590 message(1) = "Unable to read mixing information. Mixing will start from scratch."
591 call messages_warning(1, namespace=namespace)
592 end if
593 end if
594
595 if(hm%lda_u_level /= dft_u_none) then
596 call lda_u_load(restart_load, hm%lda_u, st, hm%energy%dft_u, ierr)
597 if (ierr /= 0) then
598 message(1) = "Unable to read DFT+U information. DFT+U data will be calculated from states."
599 call messages_warning(1, namespace=namespace)
600 end if
601 end if
602 else
603 if (present(outp) .and. mpi_grp_is_root(mpi_world)) then
604 call io_rm(static_dir //"info")
605 end if
606 end if
607
608 safe_allocate(rhoout(1:gr%np, 1:nspin))
609 safe_allocate(rhoin(1:gr%np, 1:nspin))
610
611 call lalg_copy(gr%np, nspin, st%rho, rhoin)
612 rhoout = m_zero
613
614 if (scf%calc_force .or. output_forces) then
615 !We store the Hxc potential for the contribution to the forces
616 safe_allocate(vhxc_old(1:gr%np, 1:nspin))
617 call lalg_copy(gr%np, nspin, hm%vhxc, vhxc_old)
618 end if
619
620
621 select case(scf%mix_field)
622 case(option__mixfield__potential)
623 call mixfield_set_vin(scf%mixfield, hm%vhxc)
624 call vtau_mixer_set_vin(scf%vtau_mix, hm)
625 case (option__mixfield__density)
626 call mixfield_set_vin(scf%mixfield, rhoin)
627
628 case(option__mixfield__states)
629
630 safe_allocate_type_array(wfs_elec_t, psioutb, (st%group%block_start:st%group%block_end, st%d%kpt%start:st%d%kpt%end))
631
632 do iqn = st%d%kpt%start, st%d%kpt%end
633 do ib = st%group%block_start, st%group%block_end
634 call st%group%psib(ib, iqn)%copy_to(psioutb(ib, iqn))
635 end do
636 end do
637
638 end select
639
640 call lda_u_update_occ_matrices(hm%lda_u, namespace, gr, st, hm%hm_base, hm%phase, hm%energy)
641 !If we use LDA+U, we also have do mix it
642 if (scf%mix_field /= option__mixfield__states) then
643 call lda_u_mixer_set_vin(hm%lda_u, scf%lda_u_mix)
644 end if
645
646 call create_convergence_file(static_dir, "convergence")
647
648 if ( verbosity_ /= verb_no ) then
649 if(scf%max_iter > 0) then
650 write(message(1),'(a)') 'Info: Starting SCF iteration.'
651 else
652 write(message(1),'(a)') 'Info: No SCF iterations will be done.'
653 ! we cannot tell whether it is converged.
654 finish = .false.
655 end if
656 call messages_info(1, namespace=namespace)
657 end if
658 converged_current = .false.
659
660 scf%matvec = 0
661
662 ! SCF cycle
663 itime = loct_clock()
664
665 do iter = 1, scf%max_iter
666 call profiling_in("SCF_CYCLE")
667
668 ! this initialization seems redundant but avoids improper optimization at -O3 by PGI 7 on chum,
669 ! which would cause a failure of testsuite/linear_response/04-vib_modes.03-vib_modes_fd.inp
670 scf%eigens%converged = 0
671
672 ! keep the information about the spectrum up to date, needed e.g. for Chebyshev expansion for imaginary time
673 call hm%update_span(gr%spacing(1:space%dim), minval(st%eigenval(:, :)), namespace)
674
675 !We update the quantities at the begining of the scf cycle
676 if (iter == 1) then
677 scf%evsum_in = states_elec_eigenvalues_sum(st)
678 end if
679 call iterator%start(scf%criterion_list)
680 do while (iterator%has_next())
681 crit => iterator%get_next()
682 call scf_update_initial_quantity(scf, hm, crit)
683 end do
684
685 if (scf%calc_force .or. output_forces) then
686 !Used for computing the imperfect convegence contribution to the forces
687 vhxc_old(1:gr%np, 1:nspin) = hm%vhxc(1:gr%np, 1:nspin)
688 end if
689
690 if(scf%lcao_restricted) then
691 call lcao_init_orbitals(lcao, namespace, st, gr, ions)
692 call lcao_wf(lcao, st, gr, ions, hm, namespace)
693 else
694
695 !We check if the system is coupled with a partner that requires self-consistency
696 ! if(hamiltonian_has_scf_partner(hm)) then
697 if (allocated(hm%vberry)) then
698 !In this case, v_Hxc is frozen and we do an internal SCF loop over the
699 ! partners that require SCF
700 ks%frozen_hxc = .true.
701 ! call perform_scf_partners()
702 call berry_perform_internal_scf(scf%berry, namespace, space, scf%eigens, gr, st, hm, iter, ks, ions, ext_partners)
703 !and we unfreeze the potential once finished
704 ks%frozen_hxc = .false.
705 else
706 scf%eigens%converged = 0
707 call scf%eigens%run(namespace, gr, st, hm, iter)
708 end if
709 end if
710
711 scf%matvec = scf%matvec + scf%eigens%matvec
712
713 ! occupations
714 call states_elec_fermi(st, namespace, gr)
715 call lda_u_update_occ_matrices(hm%lda_u, namespace, gr, st, hm%hm_base, hm%phase, hm%energy)
716
717 ! compute output density, potential (if needed) and eigenvalues sum
718 call density_calc(st, gr, st%rho)
719
720 call lalg_copy(gr%np, nspin, st%rho, rhoout)
721
722 select case (scf%mix_field)
723 case (option__mixfield__potential)
724 call v_ks_calc(ks, namespace, space, hm, st, ions, ext_partners, calc_current=output_during_scf)
725 call mixfield_set_vout(scf%mixfield, hm%vhxc)
726 call vtau_mixer_set_vout(scf%vtau_mix, hm)
727 case (option__mixfield__density)
728 call mixfield_set_vout(scf%mixfield, rhoout)
729 case(option__mixfield__states)
730
731 do iqn = st%d%kpt%start, st%d%kpt%end
732 do ib = st%group%block_start, st%group%block_end
733 call st%group%psib(ib, iqn)%copy_data_to(gr%np, psioutb(ib, iqn))
734 end do
735 end do
736 end select
737
738 if (scf%mix_field /= option__mixfield__states .and. scf%mix_field /= option__mixfield__none) then
739 call lda_u_mixer_set_vout(hm%lda_u, scf%lda_u_mix)
740 endif
741
742 ! recalculate total energy
743 call energy_calc_total(namespace, space, hm, gr, st, ext_partners, iunit = 0)
744
745 if (present(outp)) then
746 ! compute forces only if requested
747 if (outp%duringscf .and. outp%what_now(option__output__forces, iter)) then
748 call forces_calculate(gr, namespace, ions, hm, ext_partners, st, ks, vhxc_old=vhxc_old)
749 end if
750 end if
751
752 !We update the quantities at the end of the scf cycle
753 call iterator%start(scf%criterion_list)
754 do while (iterator%has_next())
755 crit => iterator%get_next()
756 call scf_update_diff_quantity(scf, hm, st, gr, rhoout, rhoin, crit)
757 end do
758
759 ! are we finished?
760 converged_last = converged_current
761
762 converged_current = scf%check_conv .and. &
763 (.not. scf%conv_eigen_error .or. all(scf%eigens%converged == st%nst))
764 !Loop over the different criteria
765 call iterator%start(scf%criterion_list)
766 do while (iterator%has_next())
767 crit => iterator%get_next()
768 call crit%is_converged(is_crit_conv)
769 converged_current = converged_current .and. is_crit_conv
770 end do
771
772 ! only finish if the convergence criteria are fulfilled in two
773 ! consecutive iterations
774 finish = converged_last .and. converged_current
775
776 etime = loct_clock() - itime
777 itime = etime + itime
778 call scf_write_iter(namespace)
779
780 ! mixing
781 select case (scf%mix_field)
782 case (option__mixfield__density)
783 ! mix input and output densities and compute new potential
784 call mixing(namespace, scf%smix)
785 call mixfield_get_vnew(scf%mixfield, st%rho)
786 ! for spinors, having components 3 or 4 be negative is not unphysical
787 if (minval(st%rho(1:gr%np, 1:st%d%spin_channels)) < -1e-6_real64) then
788 write(message(1),*) 'Negative density after mixing. Minimum value = ', &
789 minval(st%rho(1:gr%np, 1:st%d%spin_channels))
790 call messages_warning(1, namespace=namespace)
791 end if
792 call lda_u_mixer_get_vnew(hm%lda_u, scf%lda_u_mix, st)
793 call v_ks_calc(ks, namespace, space, hm, st, ions, ext_partners, calc_current=output_during_scf)
794 case (option__mixfield__potential)
795 ! mix input and output potentials
796 call mixing(namespace, scf%smix)
797 call mixfield_get_vnew(scf%mixfield, hm%vhxc)
798 call lda_u_mixer_get_vnew(hm%lda_u, scf%lda_u_mix, st)
799 call vtau_mixer_get_vnew(scf%vtau_mix, hm)
800 call hamiltonian_elec_update_pot(hm, gr)
801
802 case(option__mixfield__states)
803
804 do iqn = st%d%kpt%start, st%d%kpt%end
805 do ib = st%group%block_start, st%group%block_end
806 call batch_scal(gr%np, m_one - mix_coefficient(scf%smix), st%group%psib(ib, iqn))
807 call batch_axpy(gr%np, mix_coefficient(scf%smix), psioutb(ib, iqn), st%group%psib(ib, iqn))
808 end do
809 end do
810
811 call density_calc(st, gr, st%rho)
812 call v_ks_calc(ks, namespace, space, hm, st, ions, ext_partners, calc_current=output_during_scf)
813
814 case (option__mixfield__none)
815 call v_ks_calc(ks, namespace, space, hm, st, ions, ext_partners, calc_current=output_during_scf)
816 end select
817
818
819 ! Are we asked to stop? (Whenever Fortran is ready for signals, this should go away)
820 scf%forced_finish = clean_stop(mc%master_comm) .or. walltimer_alarm(mc%master_comm)
821
822 if (finish .and. st%modelmbparticles%nparticle > 0) then
823 call modelmb_sym_all_states(space, gr, st)
824 end if
825
826 if (present(outp) .and. present(restart_dump)) then
827 ! save restart information
828
829 if ( (finish .or. (modulo(iter, outp%restart_write_interval) == 0) &
830 .or. iter == scf%max_iter .or. scf%forced_finish) ) then
831
832 call states_elec_dump(restart_dump, space, st, gr, hm%kpoints, ierr, iter=iter)
833 if (ierr /= 0) then
834 message(1) = 'Unable to write states wavefunctions.'
835 call messages_warning(1, namespace=namespace)
836 end if
837
838 call states_elec_dump_rho(restart_dump, space, st, gr, ierr, iter=iter)
839 if (ierr /= 0) then
840 message(1) = 'Unable to write density.'
841 call messages_warning(1, namespace=namespace)
842 end if
843
844 if(hm%lda_u_level /= dft_u_none) then
845 call lda_u_dump(restart_dump, namespace, hm%lda_u, st, gr, ierr)
846 if (ierr /= 0) then
847 message(1) = 'Unable to write DFT+U information.'
848 call messages_warning(1, namespace=namespace)
849 end if
850 end if
851
852 select case (scf%mix_field)
853 case (option__mixfield__density)
854 call mix_dump(namespace, restart_dump, scf%smix, space, gr, ierr)
855 if (ierr /= 0) then
856 message(1) = 'Unable to write mixing information.'
857 call messages_warning(1, namespace=namespace)
858 end if
859 case (option__mixfield__potential)
860 call hamiltonian_elec_dump_vhxc(restart_dump, hm, space, gr, ierr)
861 if (ierr /= 0) then
862 message(1) = 'Unable to write Vhxc.'
863 call messages_warning(1, namespace=namespace)
864 end if
865
866 call mix_dump(namespace, restart_dump, scf%smix, space, gr, ierr)
867 if (ierr /= 0) then
868 message(1) = 'Unable to write mixing information.'
869 call messages_warning(1, namespace=namespace)
870 end if
871 end select
872 end if
873 end if
874
875 call write_convergence_file(static_dir, "convergence")
876
877 if(finish) then
878 if(present(iters_done)) iters_done = iter
879 if(verbosity_ >= verb_compact) then
880 write(message(1), '(a, i4, a)') 'Info: SCF converged in ', iter, ' iterations'
881 write(message(2), '(a)') ''
882 call messages_info(2, namespace=namespace)
883 end if
884 call profiling_out("SCF_CYCLE")
885 exit
886 end if
887 if (present(outp)) then
888 if (any(outp%what) .and. outp%duringscf) then
889 do what_i = lbound(outp%what, 1), ubound(outp%what, 1)
890 if (outp%what_now(what_i, iter)) then
891 write(dirname,'(a,a,i4.4)') trim(outp%iter_dir),"scf.",iter
892 call output_all(outp, namespace, space, dirname, gr, ions, iter, st, hm, ks)
893 call output_modelmb(outp, namespace, space, dirname, gr, ions, iter, st)
894 exit
895 end if
896 end do
897 end if
898 end if
899
900 ! save information for the next iteration
901 call lalg_copy(gr%np, nspin, st%rho, rhoin)
902
903 ! restart mixing
904 if (scf%mix_field /= option__mixfield__none) then
905 if (scf%smix%ns_restart > 0) then
906 if (mod(iter, scf%smix%ns_restart) == 0) then
907 message(1) = "Info: restarting mixing."
908 call messages_info(1, namespace=namespace)
909 call scf_mix_clear(scf)
910 end if
911 end if
912 end if
913
914 select case(scf%mix_field)
915 case(option__mixfield__potential)
916 call mixfield_set_vin(scf%mixfield, hm%vhxc)
917 call vtau_mixer_set_vin(scf%vtau_mix, hm)
918 case (option__mixfield__density)
919 call mixfield_set_vin(scf%mixfield, rhoin)
920 end select
921 if(scf%mix_field /= option__mixfield__states) call lda_u_mixer_set_vin(hm%lda_u, scf%lda_u_mix)
922
923 if(scf%forced_finish) then
924 call profiling_out("SCF_CYCLE")
925 exit
926 end if
927
928 ! check if debug mode should be enabled or disabled on the fly
929 call io_debug_on_the_fly(namespace)
930
931 call profiling_out("SCF_CYCLE")
932 end do !iter
933
934 if(scf%lcao_restricted) call lcao_end(lcao)
935
936 if ((scf%max_iter > 0 .and. scf%mix_field == option__mixfield__potential) .or. calc_current) then
937 call v_ks_calc(ks, namespace, space, hm, st, ions, ext_partners, &
938 calc_current=calc_current)
939 end if
940
941 select case(scf%mix_field)
942 case(option__mixfield__states)
943
944 do iqn = st%d%kpt%start, st%d%kpt%end
945 do ib = st%group%block_start, st%group%block_end
946 call psioutb(ib, iqn)%end()
947 end do
948 end do
949
950 safe_deallocate_a(psioutb)
951 end select
952
953 safe_deallocate_a(rhoout)
954 safe_deallocate_a(rhoin)
955
956 if(scf%max_iter > 0 .and. any(scf%eigens%converged < st%nst) .and. .not. scf%lcao_restricted) then
957 write(message(1),'(a)') 'Some of the states are not fully converged!'
958 call messages_warning(1, namespace=namespace)
959 end if
960
961 if(.not.finish) then
962 write(message(1), '(a,i4,a)') 'SCF *not* converged after ', iter - 1, ' iterations.'
963 call messages_warning(1, namespace=namespace)
964 end if
965
966 write(message(1), '(a,i10)') 'Info: Number of matrix-vector products: ', scf%matvec
967 call messages_info(1)
968
969 if (scf%calc_force) then
970 call forces_calculate(gr, namespace, ions, hm, ext_partners, st, ks, vhxc_old=vhxc_old)
971 end if
972
973 if (scf%calc_stress) call stress_calculate(namespace, gr, hm, st, ions, ks, ext_partners)
974
975 if(scf%max_iter == 0) then
976 call energy_calc_eigenvalues(namespace, hm, gr%der, st)
977 call states_elec_fermi(st, namespace, gr)
978 call states_elec_write_eigenvalues(st%nst, st, space, hm%kpoints, namespace=namespace)
979 end if
980
981 if(present(outp)) then
982 ! output final information
983 call scf_write_static(static_dir, "info")
984 call output_all(outp, namespace, space, static_dir, gr, ions, -1, st, hm, ks)
985 call output_modelmb(outp, namespace, space, static_dir, gr, ions, -1, st)
986 end if
987
988 if (space%is_periodic() .and. st%nik > st%d%nspin) then
989 if (bitand(hm%kpoints%method, kpoints_path) /= 0) then
990 call states_elec_write_bandstructure(static_dir, namespace, st%nst, st, &
991 ions, gr, hm%kpoints, hm%phase, vec_pot = hm%hm_base%uniform_vector_potential, &
992 vec_pot_var = hm%hm_base%vector_potential)
993 end if
994 end if
995
996 if (ks%vdw%vdw_correction == option__vdwcorrection__vdw_ts) then
997 call vdw_ts_write_c6ab(ks%vdw%vdw_ts, ions, static_dir, 'c6ab_eff', namespace)
998 end if
999
1000 safe_deallocate_a(vhxc_old)
1001
1002 pop_sub(scf_run)
1003
1004 contains
1005
1006
1007 ! ---------------------------------------------------------
1008 subroutine scf_write_iter(namespace)
1009 type(namespace_t), intent(in) :: namespace
1010
1011 character(len=50) :: str
1012 real(real64) :: dipole(1:space%dim)
1013
1014 push_sub(scf_run.scf_write_iter)
1015
1016 if ( verbosity_ == verb_full ) then
1017
1018 write(str, '(a,i5)') 'SCF CYCLE ITER #' ,iter
1019 call messages_print_with_emphasis(msg=trim(str), namespace=namespace)
1020 write(message(1),'(a,es15.8,2(a,es9.2))') ' etot = ', units_from_atomic(units_out%energy, hm%energy%total), &
1021 ' abs_ev = ', units_from_atomic(units_out%energy, scf%evsum_diff), &
1022 ' rel_ev = ', scf%evsum_diff/(abs(scf%evsum_out)+1e-20)
1023 write(message(2),'(a,es15.2,2(a,es9.2))') &
1024 ' ediff = ', scf%energy_diff, ' abs_dens = ', scf%abs_dens_diff, &
1025 ' rel_dens = ', scf%abs_dens_diff/st%qtot
1026 call messages_info(2, namespace=namespace)
1027
1028 if(.not.scf%lcao_restricted) then
1029 write(message(1),'(a,i6)') 'Matrix vector products: ', scf%eigens%matvec
1030 write(message(2),'(a,i6)') 'Converged eigenvectors: ', sum(scf%eigens%converged(1:st%nik))
1031 call messages_info(2, namespace=namespace)
1032 call states_elec_write_eigenvalues(st%nst, st, space, hm%kpoints, scf%eigens%diff, compact = .true., namespace=namespace)
1033 else
1034 call states_elec_write_eigenvalues(st%nst, st, space, hm%kpoints, compact = .true., namespace=namespace)
1035 end if
1036
1037 if (allocated(hm%vberry)) then
1038 call calc_dipole(dipole, space, gr, st, ions)
1039 call write_dipole(dipole, namespace=namespace)
1040 end if
1041
1042 if(st%d%ispin > unpolarized) then
1043 call write_magnetic_moments(gr, st, ions, gr%der%boundaries, scf%lmm_r, namespace=namespace)
1044 end if
1045
1046 if(hm%lda_u_level == dft_u_acbn0) then
1047 call lda_u_write_u(hm%lda_u, namespace=namespace)
1048 call lda_u_write_v(hm%lda_u, namespace=namespace)
1049 end if
1050
1051 write(message(1),'(a)') ''
1052 write(message(2),'(a,i5,a,f14.2)') 'Elapsed time for SCF step ', iter,':', etime
1053 call messages_info(2, namespace=namespace)
1054
1055 call scf_print_mem_use(namespace)
1056
1057 call messages_print_with_emphasis(namespace=namespace)
1058
1059 end if
1060
1061 if ( verbosity_ == verb_compact ) then
1062 write(message(1),'(a,i4,a,es15.8, a,es9.2, a, f7.1, a)') &
1063 'iter ', iter, &
1064 ' : etot ', units_from_atomic(units_out%energy, hm%energy%total), &
1065 ' : abs_dens', scf%abs_dens_diff, &
1066 ' : etime ', etime, 's'
1067 call messages_info(1, namespace=namespace)
1068 end if
1069
1070 pop_sub(scf_run.scf_write_iter)
1071 end subroutine scf_write_iter
1072
1073
1074 ! ---------------------------------------------------------
1075 subroutine scf_write_static(dir, fname)
1076 character(len=*), intent(in) :: dir, fname
1077
1078 integer :: iunit, iatom
1079 real(real64), allocatable :: hirshfeld_charges(:)
1080 real(real64) :: dipole(1:space%dim)
1081 real(real64) :: ex_virial
1082
1083 push_sub(scf_run.scf_write_static)
1084
1085 if(mpi_grp_is_root(mpi_world)) then ! this the absolute master writes
1086 call io_mkdir(dir, namespace)
1087 iunit = io_open(trim(dir) // "/" // trim(fname), namespace, action='write')
1088
1089 call grid_write_info(gr, iunit=iunit)
1090
1091 call symmetries_write_info(gr%symm, space, iunit=iunit)
1092
1093 if (space%is_periodic()) then
1094 call hm%kpoints%write_info(iunit=iunit)
1095 write(iunit,'(1x)')
1096 end if
1097
1098 call v_ks_write_info(ks, iunit=iunit)
1099
1100 ! scf information
1101 if(finish) then
1102 write(iunit, '(a, i4, a)')'SCF converged in ', iter, ' iterations'
1103 else
1104 write(iunit, '(a)') 'SCF *not* converged!'
1105 end if
1106 write(iunit, '(1x)')
1107
1108 if(any(scf%eigens%converged < st%nst) .and. .not. scf%lcao_restricted) then
1109 write(iunit,'(a)') 'Some of the states are not fully converged!'
1110 end if
1111
1112 call states_elec_write_eigenvalues(st%nst, st, space, hm%kpoints, iunit=iunit)
1113 write(iunit, '(1x)')
1114
1115 if (space%is_periodic()) then
1116 call states_elec_write_gaps(iunit, st, space)
1117 write(iunit, '(1x)')
1118 end if
1119
1120 write(iunit, '(3a)') 'Energy [', trim(units_abbrev(units_out%energy)), ']:'
1121 else
1122 iunit = 0
1123 end if
1124
1125 call energy_calc_total(namespace, space, hm, gr, st, ext_partners, iunit, full = .true.)
1126
1127 if(mpi_grp_is_root(mpi_world)) write(iunit, '(1x)')
1128 if(st%d%ispin > unpolarized) then
1129 call write_magnetic_moments(gr, st, ions, gr%der%boundaries, scf%lmm_r, iunit=iunit)
1130 if (mpi_grp_is_root(mpi_world)) write(iunit, '(1x)')
1131 end if
1132
1133 if(st%d%ispin == spinors .and. space%dim == 3 .and. &
1134 (ks%theory_level == kohn_sham_dft .or. ks%theory_level == generalized_kohn_sham_dft) ) then
1135 call write_total_xc_torque(iunit, gr, hm%vxc, st)
1136 if(mpi_grp_is_root(mpi_world)) write(iunit, '(1x)')
1137 end if
1138
1139 if(hm%lda_u_level == dft_u_acbn0) then
1140 call lda_u_write_u(hm%lda_u, iunit=iunit)
1141 call lda_u_write_v(hm%lda_u, iunit=iunit)
1142 if(mpi_grp_is_root(mpi_world)) write(iunit, '(1x)')
1143 end if
1144
1145 if(scf%calc_dipole) then
1146 call calc_dipole(dipole, space, gr, st, ions)
1147 call write_dipole(dipole, iunit=iunit)
1148 end if
1149
1150 ! This only works when we do not have a correlation part
1151 if(ks%theory_level == kohn_sham_dft .and. &
1152 hm%xc%functional(func_c,1)%family == xc_family_none .and. st%d%ispin /= spinors) then
1153 call energy_calc_virial_ex(gr%der, hm%vxc, st, ex_virial)
1154
1155 if (mpi_grp_is_root(mpi_world)) then
1156 write(iunit, '(3a)') 'Virial relation for exchange [', trim(units_abbrev(units_out%energy)), ']:'
1157 write(iunit,'(a,es14.6)') "Energy from the orbitals ", units_from_atomic(units_out%energy, hm%energy%exchange)
1158 write(iunit,'(a,es14.6)') "Energy from the potential (virial) ", units_from_atomic(units_out%energy, ex_virial)
1159 write(iunit, '(1x)')
1160 end if
1161 end if
1162
1163 if(mpi_grp_is_root(mpi_world)) then
1164 if(scf%max_iter > 0) then
1165 write(iunit, '(a)') 'Convergence:'
1166 call iterator%start(scf%criterion_list)
1167 do while (iterator%has_next())
1168 crit => iterator%get_next()
1169 call crit%write_info(iunit)
1170 end do
1171 write(iunit,'(1x)')
1172 end if
1173 ! otherwise, these values are uninitialized, and unknown.
1174
1175 if (bitand(ks%xc_family, xc_family_oep) /= 0 .and. ks%theory_level /= hartree_fock &
1176 .and. ks%theory_level /= generalized_kohn_sham_dft) then
1177 if ((ks%oep_photon%level == oep_level_full) .or. (ks%oep_photon%level == oep_level_kli)) then
1178 write(iunit, '(a)') 'Photon observables:'
1179 write(iunit, '(6x, a, es15.8,a,es15.8,a)') 'Photon number = ', ks%oep_photon%pt%number(1)
1180 write(iunit, '(6x, a, es15.8,a,es15.8,a)') 'Photon ex. = ', ks%oep_photon%pt%ex
1181 write(iunit,'(1x)')
1182 end if
1183 end if
1184
1185 if (scf%calc_force) call forces_write_info(iunit, ions, dir, namespace)
1186
1187 if (scf%calc_stress) then
1188 call output_stress(iunit, space%periodic_dim, st%stress_tensors, all_terms=.false.)
1189 call output_pressure(iunit, space%periodic_dim, st%stress_tensors%total)
1190 end if
1191
1192 end if
1193
1194 if(scf%calc_partial_charges) then
1195 safe_allocate(hirshfeld_charges(1:ions%natoms))
1196
1197 call partial_charges_calculate(gr, st, ions, hirshfeld_charges)
1198
1199 if(mpi_grp_is_root(mpi_world)) then
1200
1201 write(iunit,'(a)') 'Partial ionic charges'
1202 write(iunit,'(a)') ' Ion Hirshfeld'
1203
1204 do iatom = 1, ions%natoms
1205 write(iunit,'(i4,a10,f16.3)') iatom, trim(ions%atom(iatom)%species%get_label()), hirshfeld_charges(iatom)
1206
1207 end do
1208
1209 end if
1210
1211 safe_deallocate_a(hirshfeld_charges)
1212
1213 end if
1214
1215 if(mpi_grp_is_root(mpi_world)) then
1216 call io_close(iunit)
1217 end if
1218
1219 pop_sub(scf_run.scf_write_static)
1220 end subroutine scf_write_static
1221
1222 ! ---------------------------------------------------------
1223 subroutine write_dipole(dipole, iunit, namespace)
1224 real(real64), intent(in) :: dipole(:)
1225 integer, optional, intent(in) :: iunit
1226 type(namespace_t), optional, intent(in) :: namespace
1227
1228 push_sub(scf_run.write_dipole)
1229
1230 if(mpi_grp_is_root(mpi_world)) then
1231 call output_dipole(dipole, space%dim, iunit=iunit, namespace=namespace)
1232
1233 if (space%is_periodic()) then
1234 message(1) = "Defined only up to quantum of polarization (e * lattice vector)."
1235 message(2) = "Single-point Berry's phase method only accurate for large supercells."
1236 call messages_info(2, iunit=iunit, namespace=namespace)
1237
1238 if (hm%kpoints%full%npoints > 1) then
1239 message(1) = &
1240 "WARNING: Single-point Berry's phase method for dipole should not be used when there is more than one k-point."
1241 message(2) = "Instead, finite differences on k-points (not yet implemented) are needed."
1242 call messages_info(2, iunit=iunit, namespace=namespace)
1243 end if
1244
1245 if(.not. smear_is_semiconducting(st%smear)) then
1246 message(1) = "Single-point Berry's phase dipole calculation not correct without integer occupations."
1247 call messages_info(1, iunit=iunit, namespace=namespace)
1248 end if
1249 end if
1250
1251 call messages_info(iunit=iunit, namespace=namespace)
1252 end if
1253
1254 pop_sub(scf_run.write_dipole)
1255 end subroutine write_dipole
1256
1257 ! -----------------------------------------------------
1258
1259 subroutine create_convergence_file(dir, fname)
1260 character(len=*), intent(in) :: dir
1261 character(len=*), intent(in) :: fname
1262
1263 integer :: iunit
1264 character(len=12) :: label
1265 if(mpi_grp_is_root(mpi_world)) then ! this the absolute master writes
1266 call io_mkdir(dir, namespace)
1267 iunit = io_open(trim(dir) // "/" // trim(fname), namespace, action='write')
1268 write(iunit, '(a)', advance = 'no') '#iter energy '
1269 label = 'energy_diff'
1270 write(iunit, '(1x,a)', advance = 'no') label
1271 label = 'abs_dens'
1272 write(iunit, '(1x,a)', advance = 'no') label
1273 label = 'rel_dens'
1274 write(iunit, '(1x,a)', advance = 'no') label
1275 label = 'abs_ev'
1276 write(iunit, '(1x,a)', advance = 'no') label
1277 label = 'rel_ev'
1278 write(iunit, '(1x,a)', advance = 'no') label
1279 if (bitand(ks%xc_family, xc_family_oep) /= 0 .and. ks%theory_level /= hartree_fock &
1280 .and. ks%theory_level /= generalized_kohn_sham_dft) then
1281 if (ks%oep%level == oep_level_full) then
1282 label = 'OEP norm2ss'
1283 write(iunit, '(1x,a)', advance = 'no') label
1284 end if
1285 end if
1286 write(iunit,'(a)') ''
1287 call io_close(iunit)
1288 end if
1289
1290 end subroutine create_convergence_file
1291
1292 ! -----------------------------------------------------
1293
1294 subroutine write_convergence_file(dir, fname)
1295 character(len=*), intent(in) :: dir
1296 character(len=*), intent(in) :: fname
1297
1298 integer :: iunit
1299
1300 if(mpi_grp_is_root(mpi_world)) then ! this the absolute master writes
1301 call io_mkdir(dir, namespace)
1302 iunit = io_open(trim(dir) // "/" // trim(fname), namespace, action='write', position='append')
1303 write(iunit, '(i5,es18.8)', advance = 'no') iter, units_from_atomic(units_out%energy, hm%energy%total)
1304 call iterator%start(scf%criterion_list)
1305 do while (iterator%has_next())
1306 crit => iterator%get_next()
1307 select type (crit)
1308 type is (energy_criterion_t)
1309 write(iunit, '(es13.5)', advance = 'no') units_from_atomic(units_out%energy, crit%val_abs)
1310 type is (density_criterion_t)
1311 write(iunit, '(2es13.5)', advance = 'no') crit%val_abs, crit%val_rel
1312 type is (eigenval_criterion_t)
1313 write(iunit, '(es13.5)', advance = 'no') units_from_atomic(units_out%energy, crit%val_abs)
1314 write(iunit, '(es13.5)', advance = 'no') crit%val_rel
1315 class default
1316 assert(.false.)
1317 end select
1318 end do
1319 if (bitand(ks%xc_family, xc_family_oep) /= 0 .and. ks%theory_level /= hartree_fock &
1320 .and. ks%theory_level /= generalized_kohn_sham_dft) then
1321 if (ks%oep%level == oep_level_full) then
1322 write(iunit, '(es13.5)', advance = 'no') ks%oep%norm2ss
1323 end if
1324 end if
1325 write(iunit,'(a)') ''
1326 call io_close(iunit)
1327 end if
1328
1329 end subroutine write_convergence_file
1330
1331 end subroutine scf_run
1332
1333 ! ---------------------------------------------------------
1334 subroutine scf_state_info(namespace, st)
1335 type(namespace_t), intent(in) :: namespace
1336 class(states_abst_t), intent(in) :: st
1337
1338 push_sub(scf_state_info)
1339
1340 if (states_are_real(st)) then
1341 call messages_write('Info: SCF using real wavefunctions.')
1342 else
1343 call messages_write('Info: SCF using complex wavefunctions.')
1344 end if
1345 call messages_info(namespace=namespace)
1346
1347 pop_sub(scf_state_info)
1348
1349 end subroutine scf_state_info
1350
1351 ! ---------------------------------------------------------
1352 subroutine scf_print_mem_use(namespace)
1353 type(namespace_t), intent(in) :: namespace
1354 real(real64) :: mem
1355 real(real64) :: mem_tmp
1356
1357 push_sub(scf_print_mem_use)
1358
1359 if(conf%report_memory) then
1360 mem = loct_get_memory_usage()/(1024.0_real64**2)
1361 call mpi_world%allreduce(mem, mem_tmp, 1, mpi_double_precision, mpi_sum)
1362 mem = mem_tmp
1363 write(message(1),'(a,f14.2)') 'Memory usage [Mbytes] :', mem
1364 call messages_info(1, namespace=namespace)
1365 end if
1366
1367 pop_sub(scf_print_mem_use)
1368 end subroutine scf_print_mem_use
1369
1370 ! --------------------------------------------------------
1372 subroutine scf_update_initial_quantity(scf, hm, criterion)
1373 type(scf_t), intent(inout) :: scf
1374 type(hamiltonian_elec_t), intent(in) :: hm
1375 class(convergence_criterion_t), intent(in) :: criterion
1376
1378
1379 select type (criterion)
1380 type is (energy_criterion_t)
1381 scf%energy_in = hm%energy%total
1382 type is (density_criterion_t)
1383 !Do nothing here
1384 type is (eigenval_criterion_t)
1385 !Setting of the value is done in the scf_update_diff_quantity routine
1386 class default
1387 assert(.false.)
1388 end select
1389
1391 end subroutine scf_update_initial_quantity
1392
1393 ! --------------------------------------------------------
1395 subroutine scf_update_diff_quantity(scf, hm, st, gr, rhoout, rhoin, criterion)
1396 type(scf_t), intent(inout) :: scf
1397 type(hamiltonian_elec_t), intent(in) :: hm
1398 type(states_elec_t), intent(in) :: st
1399 type(grid_t), intent(in) :: gr
1400 real(real64), intent(in) :: rhoout(:,:), rhoin(:,:)
1401 class(convergence_criterion_t), intent(in) :: criterion
1402
1403 integer :: is
1404 real(real64), allocatable :: tmp(:)
1405
1406 push_sub(scf_update_diff_quantity)
1407
1408 select type (criterion)
1409 type is (energy_criterion_t)
1410 scf%energy_diff = abs(hm%energy%total - scf%energy_in)
1411
1412 type is (density_criterion_t)
1413 scf%abs_dens_diff = m_zero
1414 safe_allocate(tmp(1:gr%np))
1415 do is = 1, st%d%nspin
1416 tmp = abs(rhoin(1:gr%np, is) - rhoout(1:gr%np, is))
1417 scf%abs_dens_diff = scf%abs_dens_diff + dmf_integrate(gr, tmp)
1418 end do
1419 safe_deallocate_a(tmp)
1420
1421 type is (eigenval_criterion_t)
1422 scf%evsum_out = states_elec_eigenvalues_sum(st)
1423 scf%evsum_diff = abs(scf%evsum_out - scf%evsum_in)
1424 scf%evsum_in = scf%evsum_out
1425
1426 class default
1427 assert(.false.)
1428 end select
1429
1431 end subroutine scf_update_diff_quantity
1432
1433
1434end module scf_oct_m
1435
1436
1437!! Local Variables:
1438!! mode: f90
1439!! coding: utf-8
1440!! End:
batchified version of the BLAS axpy routine:
Definition: batch_ops.F90:152
scale a batch by a constant or vector
Definition: batch_ops.F90:160
Copies a vector x, to a vector y.
Definition: lalg_basic.F90:185
This module implements common operations on batches of mesh functions.
Definition: batch_ops.F90:116
subroutine, public berry_perform_internal_scf(this, namespace, space, eigensolver, gr, st, hm, iter, ks, ions, ext_partners)
Definition: berry.F90:184
subroutine, public berry_init(this, namespace)
Definition: berry.F90:159
subroutine, public calc_dipole(dipole, space, mesh, st, ions)
Definition: berry.F90:250
subroutine, public criteria_factory_init(list, namespace, check_conv)
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
subroutine, public eigensolver_init(eigens, namespace, gr, st, mc, space)
integer, parameter, public rs_evo
subroutine, public eigensolver_end(eigens)
integer, parameter, public unpolarized
Parameters...
integer, parameter, public spinors
subroutine, public energy_calc_total(namespace, space, hm, gr, st, ext_partners, iunit, full)
This subroutine calculates the total energy of the system. Basically, it adds up the KS eigenvalues,...
subroutine, public energy_calc_virial_ex(der, vxc, st, ex)
subroutine, public energy_calc_eigenvalues(namespace, hm, der, st)
subroutine, public forces_write_info(iunit, ions, dir, namespace)
Definition: forces.F90:580
subroutine, public forces_calculate(gr, namespace, ions, hm, ext_partners, st, ks, vhxc_old, t, dt)
Definition: forces.F90:336
real(real64), parameter, public m_zero
Definition: global.F90:187
real(real64), parameter, public lmm_r_single_atom
Default local magnetic moments sphere radius for an isolated system.
Definition: global.F90:215
type(conf_t), public conf
Global instance of Octopus configuration.
Definition: global.F90:177
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 generalized_kohn_sham_dft
integer, parameter, public hartree_fock
integer, parameter, public independent_particles
subroutine, public hamiltonian_elec_dump_vhxc(restart, hm, space, mesh, ierr)
subroutine, public hamiltonian_elec_update_pot(this, mesh, accumulate)
Update the KS potential of the electronic Hamiltonian.
subroutine, public hamiltonian_elec_load_vhxc(restart, hm, space, mesh, ierr)
integer, parameter, public kohn_sham_dft
This module defines classes and functions for interaction partners.
Definition: io.F90:114
subroutine, public io_close(iunit, grp)
Definition: io.F90:468
subroutine, public io_debug_on_the_fly(namespace)
check if debug mode should be enabled or disabled on the fly
Definition: io.F90:568
subroutine, public io_rm(fname, namespace)
Definition: io.F90:385
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
integer, parameter, public kpoints_path
Definition: kpoints.F90:209
subroutine, public lcao_init_orbitals(this, namespace, st, gr, ions, start)
Definition: lcao.F90:1519
subroutine, public lcao_wf(this, st, gr, ions, hm, namespace, start)
Definition: lcao.F90:993
subroutine, public lcao_end(this)
Definition: lcao.F90:963
subroutine, public lcao_init(this, namespace, space, gr, ions, st)
Definition: lcao.F90:242
logical function, public lcao_is_available(this)
Definition: lcao.F90:1031
subroutine, public lda_u_dump(restart, namespace, this, st, mesh, ierr)
Definition: lda_u_io.F90:646
subroutine, public lda_u_write_u(this, iunit, namespace)
Definition: lda_u_io.F90:530
subroutine, public lda_u_load(restart, this, st, dftu_energy, ierr, occ_only, u_only)
Definition: lda_u_io.F90:731
subroutine, public lda_u_write_v(this, iunit, namespace)
Definition: lda_u_io.F90:579
subroutine, public lda_u_mixer_set_vin(this, mixer)
subroutine, public lda_u_mixer_init(this, mixer, st)
subroutine, public lda_u_mixer_clear(mixer, smix)
subroutine, public lda_u_mixer_init_auxmixer(this, namespace, mixer, smix, st)
subroutine, public lda_u_mixer_get_vnew(this, mixer, st)
subroutine, public lda_u_mixer_set_vout(this, mixer)
subroutine, public lda_u_mixer_end(mixer, smix)
integer, parameter, public dft_u_none
Definition: lda_u.F90:200
integer, parameter, public dft_u_acbn0
Definition: lda_u.F90:200
subroutine, public lda_u_update_occ_matrices(this, namespace, mesh, st, hm_base, phase, energy)
Definition: lda_u.F90:796
subroutine, public write_magnetic_moments(mesh, st, ions, boundaries, lmm_r, iunit, namespace)
Definition: magnetic.F90:211
subroutine, public write_total_xc_torque(iunit, mesh, vxc, st)
Definition: magnetic.F90:431
This module is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:115
This module defines various routines, operating on mesh functions.
real(real64) function, public dmf_integrate(mesh, ff, mask, reduce)
Integrate a function on the mesh.
This module defines the meshes, which are used in Octopus.
Definition: mesh.F90:118
subroutine, public messages_print_with_emphasis(msg, iunit, namespace)
Definition: messages.F90:930
character(len=512), private msg
Definition: messages.F90:165
subroutine, public messages_warning(no_lines, all_nodes, namespace)
Definition: messages.F90:543
subroutine, public messages_obsolete_variable(namespace, name, rep)
Definition: messages.F90:1057
subroutine, public messages_info(no_lines, iunit, verbose_limit, stress, all_nodes, namespace)
Definition: messages.F90:624
subroutine, public messages_new_line()
Definition: messages.F90:1146
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:160
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)
Definition: messages.F90:420
subroutine, public messages_input_error(namespace, var, details, row, column)
Definition: messages.F90:723
subroutine, public messages_experimental(name, namespace)
Definition: messages.F90:1097
real(real64) pure function, public mix_coefficient(this)
Definition: mix.F90:774
subroutine, public mixing(namespace, smix)
Main entry-point to SCF mixer.
Definition: mix.F90:800
subroutine, public mix_get_field(this, mixfield)
Definition: mix.F90:792
subroutine, public mix_dump(namespace, restart, smix, space, mesh, ierr)
Definition: mix.F90:543
subroutine, public mix_load(namespace, restart, smix, space, mesh, ierr)
Definition: mix.F90:645
subroutine, public mix_init(smix, namespace, space, der, d1, d2, def_, func_type_, prefix_)
Initialise mix_t instance.
Definition: mix.F90:265
subroutine, public mix_end(smix)
Definition: mix.F90:522
subroutine, public mix_clear(smix)
Definition: mix.F90:507
subroutine, public modelmb_sym_all_states(space, mesh, st)
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
logical function, public output_needs_current(outp, states_are_real)
Definition: output.F90:980
subroutine, public output_all(outp, namespace, space, dir, gr, ions, iter, st, hm, ks)
Definition: output.F90:491
subroutine, public partial_charges_calculate(mesh, st, ions, hirshfeld_charges)
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
logical function, public clean_stop(comm)
returns true if a file named stop exists
Definition: restart.F90:274
integer, parameter, public restart_flag_mix
Definition: restart.F90:247
integer, parameter, public restart_flag_rho
Definition: restart.F90:247
integer, parameter, public restart_flag_vhxc
Definition: restart.F90:247
logical pure function, public restart_has_flag(restart, flag)
Returns true if...
Definition: restart.F90:977
subroutine scf_update_initial_quantity(scf, hm, criterion)
Update the quantity at the begining of a SCF cycle.
Definition: scf.F90:1466
subroutine scf_update_diff_quantity(scf, hm, st, gr, rhoout, rhoin, criterion)
Update the quantity at the begining of a SCF cycle.
Definition: scf.F90:1489
subroutine, public scf_state_info(namespace, st)
Definition: scf.F90:1428
subroutine, public scf_print_mem_use(namespace)
Definition: scf.F90:1446
subroutine, public scf_mix_clear(scf)
Definition: scf.F90:556
subroutine, public scf_run(scf, namespace, space, mc, gr, ions, ext_partners, st, ks, hm, outp, verbosity, iters_done, restart_load, restart_dump)
Definition: scf.F90:574
integer, parameter, public verb_full
Definition: scf.F90:197
integer, parameter, public verb_compact
Definition: scf.F90:197
subroutine, public scf_init(scf, namespace, gr, ions, st, mc, hm, space)
Definition: scf.F90:237
subroutine, public scf_end(scf)
Definition: scf.F90:526
logical pure function, public smear_is_semiconducting(this)
Definition: smear.F90:829
pure logical function, public states_are_real(st)
This module defines routines to write information about states.
subroutine, public states_elec_write_eigenvalues(nst, st, space, kpoints, error, st_start, compact, iunit, namespace)
write the eigenvalues for some states to a file.
subroutine, public states_elec_write_gaps(iunit, st, space)
calculate gaps and write to a file.
subroutine, public states_elec_write_bandstructure(dir, namespace, nst, st, ions, mesh, kpoints, phase, vec_pot, vec_pot_var)
calculate and write the bandstructure
subroutine, public states_elec_fermi(st, namespace, mesh, compute_spin)
calculate the Fermi level for the states in this object
real(real64) function, public states_elec_eigenvalues_sum(st, alt_eig)
function to calculate the eigenvalues sum using occupations as weights
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)
subroutine, public states_elec_load_rho(restart, space, st, mesh, ierr)
subroutine, public states_elec_dump_rho(restart, space, st, mesh, ierr, iter)
This module implements the calculation of the stress tensor.
Definition: stress.F90:118
subroutine, public output_pressure(iunit, space_dim, total_stress_tensor)
Definition: stress.F90:1099
subroutine, public stress_calculate(namespace, gr, hm, st, ions, ks, ext_partners)
This computes the total stress on the lattice.
Definition: stress.F90:184
subroutine, public output_stress(iunit, space_dim, stress_tensors, all_terms)
Definition: stress.F90:1033
subroutine, public symmetries_write_info(this, space, iunit, namespace)
Definition: symmetries.F90:617
type(type_t), public type_float
Definition: types.F90:133
brief This module defines the class unit_t which is used by the unit_systems_oct_m module.
Definition: unit.F90:132
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
type(unit_system_t), public units_inp
the units systems for reading and writing
This module is intended to contain simple general-purpose utility functions and procedures.
Definition: utils.F90:118
subroutine, public output_dipole(dipole, ndim, iunit, namespace)
Definition: utils.F90:278
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
Tkatchenko-Scheffler pairwise method for van der Waals (vdW, dispersion) interactions.
Definition: vdw_ts.F90:119
subroutine, public vdw_ts_write_c6ab(this, ions, dir, fname, namespace)
Definition: vdw_ts.F90:548
subroutine, public vtau_mixer_end(mixer, smix)
Definition: vtau_mixer.F90:186
subroutine, public vtau_mixer_init_auxmixer(namespace, mixer, smix, hm, np, nspin)
Definition: vtau_mixer.F90:149
subroutine, public vtau_mixer_set_vout(mixer, hm)
Definition: vtau_mixer.F90:199
subroutine, public vtau_mixer_get_vnew(mixer, hm)
Definition: vtau_mixer.F90:225
subroutine, public vtau_mixer_clear(mixer, smix)
Definition: vtau_mixer.F90:173
subroutine, public vtau_mixer_set_vin(mixer, hm)
Definition: vtau_mixer.F90:212
This module provices a simple timer class which can be used to trigger the writing of a restart file ...
Definition: walltimer.F90:121
logical function, public walltimer_alarm(comm, print)
indicate whether time is up
Definition: walltimer.F90:303
integer, parameter, public xc_family_nc_mgga
integer, parameter, public func_c
Definition: xc.F90:114
integer, parameter, public oep_level_full
Definition: xc_oep.F90:172
integer, parameter, public oep_level_kli
Definition: xc_oep.F90:172
subroutine scf_write_static(dir, fname)
Definition: rdmft.F90:586
subroutine write_dipole(dipole, iunit, namespace)
Definition: scf.F90:1317
subroutine create_convergence_file(dir, fname)
Definition: scf.F90:1353
subroutine scf_write_iter(namespace)
Definition: scf.F90:1102
subroutine write_convergence_file(dir, fname)
Definition: scf.F90:1388
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
Stores all communicators and groups.
Definition: multicomm.F90:206
output handler class
Definition: output_low.F90:163
some variables used for the SCF cycle
Definition: scf.F90:203
abstract class for states
The states_elec_t class contains all electronic wave functions.
batches of electronic states
Definition: wfs_elec.F90:138
int true(void)