forked from iris-theof/RDMFT_Octopus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdmft.F90
1523 lines (1243 loc) · 52.3 KB
/
rdmft.F90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!! Copyright (C) 2012-2019 I. Theophilou, N. Helbig
!! Copyright (C) 2019 F. Buchholz, M. Oliveira
!!
!! This program is free software; you can redistribute it and/or modify
!! it under the terms of the GNU General Public License as published by
!! the Free Software Foundation; either version 2, or (at your option)
!! any later version.
!!
!! This program is distributed in the hope that it will be useful,
!! but WITHOUT ANY WARRANTY; without even the implied warranty of
!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
!! GNU General Public License for more details.
!!
!! You should have received a copy of the GNU General Public License
!! along with this program; if not, write to the Free Software
!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
!! 02110-1301, USA.
!!
#include "global.h"
module rdmft_oct_m
use density_oct_m
use derivatives_oct_m
use eigen_cg_oct_m
use eigensolver_oct_m
use energy_oct_m
use geometry_oct_m
use global_oct_m
use grid_oct_m
use hamiltonian_elec_oct_m
use hamiltonian_elec_base_oct_m
use io_oct_m
use io_function_oct_m
use lalg_adv_oct_m
use loct_oct_m
use mesh_oct_m
use mesh_function_oct_m
use messages_oct_m
use minimizer_oct_m
use mpi_oct_m
use mpi_lib_oct_m
use multicomm_oct_m
use namespace_oct_m
use output_oct_m
use parser_oct_m
use photon_mode_oct_m
use poisson_oct_m
use profiling_oct_m
use restart_oct_m
use simul_box_oct_m
use species_oct_m
use states_abst_oct_m
use states_elec_oct_m
use states_elec_calc_oct_m
use states_elec_restart_oct_m
use unit_oct_m
use unit_system_oct_m
use v_ks_oct_m
use xc_oep_oct_m
implicit none
private
public :: &
rdm_t, &
rdmft_init, &
rdmft_end, &
scf_rdmft
type rdm_t
private
type(eigensolver_t) :: eigens
integer :: max_iter !< maximum number of scf iterations
integer :: iter
integer :: nst !< number of states
integer :: n_twoint !number of unique two electron integrals
logical :: do_basis
logical :: hf
FLOAT :: mu
FLOAT :: occsum
FLOAT :: qtot
FLOAT :: scale_f
FLOAT :: toler
FLOAT :: conv_ener
FLOAT :: maxFO
FLOAT :: tolerFO
FLOAT, allocatable :: eone(:)
FLOAT, allocatable :: eone_int(:,:)
FLOAT, allocatable :: twoint(:)
FLOAT, allocatable :: hartree(:,:)
FLOAT, allocatable :: exchange(:,:)
FLOAT, allocatable :: evalues(:)
FLOAT, allocatable :: vecnat(:,:)
FLOAT, allocatable :: Coul(:,:,:)
FLOAT, allocatable :: Exch(:,:,:)
integer, allocatable :: i_index(:,:)
integer, allocatable :: j_index(:,:)
integer, allocatable :: k_index(:,:)
integer, allocatable :: l_index(:,:)
end type rdm_t
type(rdm_t), pointer :: rdm_ptr
contains
! ---------------------------------------------------------
subroutine rdmft_init(rdm, namespace, gr, st, geo, mc, fromScratch)
type(rdm_t), intent(out) :: rdm
type(namespace_t), intent(in) :: namespace
type(grid_t), intent(inout) :: gr !< grid
type(states_elec_t), intent(in) :: st !< States
type(geometry_t), intent(in) :: geo
type(multicomm_t), intent(in) :: mc
logical, intent(in) :: fromScratch
integer :: ist
PUSH_SUB(rdmft_init)
if(st%nst < st%qtot + 1) then
message(1) = "Too few states to run RDMFT calculation"
message(2) = "Number of states should be at least the number of electrons plus one"
call messages_fatal(2)
end if
if (states_are_complex(st)) then
call messages_not_implemented("Complex states for RDMFT")
end if
! The documentation for the variable is found in scf_init.
call parse_variable(namespace, 'MaximumIter', 200, rdm%max_iter)
!%Variable RDMTolerance
!%Type float
!%Default 1e-7 Ha
!%Section SCF::RDMFT
!%Description
!% Convergence criterion for stopping the occupation numbers minimization. Minimization is
!% stopped when all derivatives of the energy wrt. each occupation number
!% are smaller than this criterion. The bisection for finding the correct mu that is needed
!% for the occupation number minimization also stops according to this criterion.
!%End
call parse_variable(namespace, 'RDMTolerance', CNST(1.0e-7), rdm%toler)
!%Variable RDMToleranceFO
!%Type float
!%Default 1e-4 Ha
!%Section SCF::RDMFT
!%Description
!% Convergence criterion for stopping the diagonalization of the Fock matrix in the Piris method.
!% Orbital minimization is stopped when all off-diagonal ellements of the Fock matrix
!% are smaller than this criterion.
!%End
call parse_variable(namespace, 'RDMToleranceFO', CNST(1.0e-4), rdm%tolerFO)
!%Variable RDMConvEner
!%Type float
!%Default 1e-6 Ha
!%Section SCF::RDMFT
!%Description
!% Convergence criterion for stopping the overall minimization of the energy with
!% respect to occupation numbers and the orbitals. The minimization of the
!% energy stops when the total energy difference between two subsequent
!% minimizations of the energy with respect to the occupation numbers and the
!% orbitals is smaller than this criterion. It is also used to exit the orbital minimization.
!%End
call parse_variable(namespace, 'RDMConvEner', CNST(1.0e-7), rdm%conv_ener)
!%Variable RDMBasis
!%Type logical
!%Default yes
!%Section SCF::RDMFT
!%Description
!% If true, all the energy terms and corresponding derivatives involved in RDMFT will
!% not be calculated on the grid but on the basis of the initial orbitals
!%End
call parse_variable(namespace, 'RDMBasis',.true., rdm%do_basis)
if (rdm%do_basis .and. fromScratch) then
call messages_write("RDMFT calculations with RDMBasis = yes cannot be started FromScratch", new_line=.true.)
call messages_write("Run a calculation for independent particles first")
call messages_fatal()
end if
!%Variable RDMHartreeFock
!%Type logical
!%Default no
!%Section SCF::RDMFT
!%Description
!% If true, the code simulates a HF calculation, by omitting the occ.num. optimization
!% can be used for test reasons
!%End
call parse_variable(namespace, 'RDMHartreeFock',.false., rdm%hf)
rdm%nst = st%nst
if (rdm%do_basis) then
rdm%n_twoint = rdm%nst*(rdm%nst + 1)*(rdm%nst**2 + rdm%nst + 2)/8
SAFE_ALLOCATE(rdm%eone_int(1:rdm%nst, 1:rdm%nst))
SAFE_ALLOCATE(rdm%twoint(1:rdm%n_twoint))
SAFE_ALLOCATE(rdm%i_index(1:2,1:rdm%n_twoint))
SAFE_ALLOCATE(rdm%j_index(1:2,1:rdm%n_twoint))
SAFE_ALLOCATE(rdm%k_index(1:2,1:rdm%n_twoint))
SAFE_ALLOCATE(rdm%l_index(1:2,1:rdm%n_twoint))
SAFE_ALLOCATE(rdm%vecnat(1:rdm%nst, 1:rdm%nst))
SAFE_ALLOCATE(rdm%Coul(1:rdm%nst, 1:rdm%nst, 1:rdm%nst))
SAFE_ALLOCATE(rdm%Exch(1:rdm%nst, 1:rdm%nst, 1:rdm%nst))
rdm%eone_int = M_ZERO
rdm%twoint = M_ZERO
rdm%vecnat = M_ZERO
rdm%i_index = M_ZERO
rdm%j_index = M_ZERO
rdm%k_index = M_ZERO
rdm%l_index = M_ZERO
rdm%Coul = M_ZERO
rdm%Exch = M_ZERO
do ist = 1, rdm%nst
rdm%vecnat(ist, ist) = M_ONE
end do
else
! initialize eigensolver.
call eigensolver_init(rdm%eigens, namespace, gr, st, geo, mc)
if (rdm%eigens%additional_terms) call messages_not_implemented("CG Additional Terms with RDMFT.")
end if
SAFE_ALLOCATE(rdm%eone(1:rdm%nst))
SAFE_ALLOCATE(rdm%hartree(1:rdm%nst, 1:rdm%nst))
SAFE_ALLOCATE(rdm%exchange(1:rdm%nst, 1:rdm%nst))
SAFE_ALLOCATE(rdm%evalues(1:rdm%nst))
rdm%eone = M_ZERO
rdm%hartree = M_ZERO
rdm%exchange = M_ZERO
rdm%evalues = M_ZERO
rdm%mu = M_TWO*st%eigenval(int(st%qtot*M_HALF), 1)
rdm%qtot = st%qtot
rdm%occsum = M_ZERO
rdm%scale_f = CNST(1e-2)
rdm%maxFO = M_ZERO
rdm%iter = 0
POP_SUB(rdmft_init)
end subroutine rdmft_init
! ----------------------------------------
subroutine rdmft_end(rdm, gr)
type(rdm_t), intent(inout) :: rdm
type(grid_t), intent(inout) :: gr
PUSH_SUB(rdmft_end)
SAFE_DEALLOCATE_A(rdm%evalues)
SAFE_DEALLOCATE_A(rdm%eone)
SAFE_DEALLOCATE_A(rdm%hartree)
SAFE_DEALLOCATE_A(rdm%exchange)
if (rdm%do_basis) then
SAFE_DEALLOCATE_A(rdm%eone_int)
SAFE_DEALLOCATE_A(rdm%twoint)
SAFE_DEALLOCATE_A(rdm%i_index)
SAFE_DEALLOCATE_A(rdm%j_index)
SAFE_DEALLOCATE_A(rdm%k_index)
SAFE_DEALLOCATE_A(rdm%l_index)
SAFE_DEALLOCATE_A(rdm%vecnat)
SAFE_DEALLOCATE_A(rdm%Coul)
SAFE_DEALLOCATE_A(rdm%Exch)
else
call eigensolver_end(rdm%eigens, gr)
end if
POP_SUB(rdmft_end)
end subroutine rdmft_end
! ----------------------------------------
! scf for the occupation numbers and the natural orbitals
subroutine scf_rdmft(rdm, namespace, gr, geo, st, ks, hm, outp, restart_dump)
type(rdm_t), intent(inout) :: rdm
type(namespace_t), intent(in) :: namespace
type(grid_t), intent(in) :: gr !< grid
type(geometry_t), intent(in) :: geo !< geometry
type(states_elec_t), intent(inout) :: st !< States
type(v_ks_t), intent(inout) :: ks !< Kohn-Sham
type(hamiltonian_elec_t), intent(inout) :: hm !< Hamiltonian
type(output_t), intent(in) :: outp !< output
type(restart_t), intent(in) :: restart_dump
type(states_elec_t) :: states_save
integer :: iter, icount, ip, ist, ierr, maxcount, iorb
FLOAT :: energy, energy_dif, energy_old, energy_occ, xpos, xneg, rel_ener
FLOAT, allocatable :: dpsi(:,:), dpsi2(:,:)
logical :: conv
character(len=MAX_PATH_LEN) :: dirname
PUSH_SUB(scf_rdmft)
if (hm%d%ispin /= 1) then
call messages_not_implemented("RDMFT exchange function not yet implemented for spin_polarized or spinors")
end if
! problem is about k-points for exchange
if (simul_box_is_periodic(gr%sb)) then
call messages_not_implemented("Periodic system calculations for RDMFT", namespace=namespace)
end if
! exchange routine needs all states on each processor currently
if(st%parallel_in_states) then
call messages_not_implemented("RDMFT parallel in states", namespace=namespace)
end if
call messages_print_stress(stdout, 'RDMFT Calculation', namespace=namespace)
call messages_print_var_value(stdout, 'RDMBasis', rdm%do_basis)
!set initial values
energy_old = CNST(1.0e20)
xpos = M_ZERO
xneg = M_ZERO
energy = M_ZERO
if (.not. rdm%do_basis) then
maxcount = 1 !still needs to be checked
else
maxcount = 50
!precalculate matrix elements in basis
write(message(1),'(a)') 'Calculating Coulomb and exchange matrix elements in basis'
write(message(2),'(a)') '--this may take a while--'
call messages_info(2)
call dstates_elec_me_two_body(st, namespace, gr, hm%exxop%psolver, 1, st%nst, rdm%i_index, rdm%j_index, rdm%k_index, &
rdm%l_index, rdm%twoint)
call rdm_integrals(rdm, namespace, hm, st, gr%mesh)
call sum_integrals(rdm)
endif
! Start the actual minimization, first step is minimization of occupation numbers
! Orbital minimization is according to Piris and Ugalde, Vol. 30, No. 13, J. Comput. Chem. (scf_orb) or
! using conjugated gradient (scf_orb_cg)
do iter = 1, rdm%max_iter
rdm%iter = rdm%iter + 1
write(message(1), '(a)') '**********************************************************************'
write(message(2),'(a, i4)') 'Iteration:', iter
call messages_info(2)
! occupation number optimization unless we are doing Hartree-Fock
if (rdm%hf) then
call scf_occ_NO(rdm, namespace, gr, hm, st, energy_occ)
else
call scf_occ(rdm, namespace, gr, hm, st, energy_occ)
end if
! orbital optimization
write(message(1), '(a)') 'Optimization of natural orbitals'
call messages_info(1)
do icount = 1, maxcount
if (rdm%do_basis) then
call scf_orb(rdm, namespace, gr, st, hm, energy)
else
call scf_orb_cg(rdm, namespace, gr, geo, st, ks, hm, energy)
end if
energy_dif = energy - energy_old
energy_old = energy
if (rdm%do_basis) then
if (abs(energy_dif)/abs(energy) < rdm%conv_ener .and. rdm%maxFO < rdm%tolerFO) exit
if (energy_dif < M_ZERO) then
xneg = xneg + 1
else
xpos = xpos + 1
end if
if (xneg > CNST(1.5e0)*xpos) then
rdm%scale_f = CNST(1.01)*rdm%scale_f
elseif (xneg < CNST(1.1e0)*xpos) then
rdm%scale_f = CNST(0.95)* rdm%scale_f
end if
endif !rdm%do_basis
end do !icount
xneg = M_ZERO
xpos = M_ZERO
rel_ener = abs(energy_occ-energy)/abs(energy)
write(message(1),'(a,11x,es20.10)') 'Total energy:', units_from_atomic(units_out%energy,energy + hm%ep%eii)
write(message(2),'(a,1x,es20.10)') 'Rel. energy difference:', rel_ener
call messages_info(2)
if (.not. rdm%hf .and. rdm%do_basis) then
write(message(1),'(a,18x,es20.10)') 'Max F0:', rdm%maxFO
call messages_info(1)
end if
if (rdm%do_basis) then
conv = (rel_ener < rdm%conv_ener) .and. rdm%maxFO < rdm%tolerFO
else
conv = rel_ener < rdm%conv_ener
endif
if (rdm%toler > CNST(1e-4)) rdm%toler = rdm%toler*CNST(1e-1) !Is this still okay or does it restrict the possible convergence? FB: Does this makes sense at all?
! save restart information
if ((conv .or. (modulo(iter, outp%restart_write_interval) == 0) .or. iter == rdm%max_iter)) then
if (rdm%do_basis) then
call states_elec_copy(states_save, st)
SAFE_ALLOCATE(dpsi(1:gr%mesh%np, 1:st%d%dim))
SAFE_ALLOCATE(dpsi2(1:gr%mesh%np, 1:st%d%dim))
do iorb = 1, st%nst
dpsi = M_ZERO
do ist = 1, st%nst
call states_elec_get_state(st, gr%mesh, ist, 1, dpsi2)
do ip = 1, gr%mesh%np
dpsi(ip,1) = dpsi(ip,1) + rdm%vecnat(ist, iorb)*dpsi2(ip,1)
end do
end do
call states_elec_set_state(states_save, gr%mesh, iorb, 1, dpsi)
end do
call density_calc(states_save, gr, states_save%rho)
! if other quantities besides the densities and the states are needed they also have to be recalculated here!
call states_elec_dump(restart_dump, states_save, gr, ierr, iter=iter)
if (conv .or. iter == rdm%max_iter) then
call states_elec_end(st)
call states_elec_copy(st, states_save)
end if
call states_elec_end(states_save)
SAFE_DEALLOCATE_A(dpsi)
SAFE_DEALLOCATE_A(dpsi2)
else
call states_elec_dump(restart_dump, st, gr, ierr, iter=iter)
! calculate maxFO for cg-solver
if (.not. rdm%hf) then
call calc_maxFO (namespace, hm, st, gr, rdm)
write(message(1),'(a,18x,es20.10)') 'Max F0:', rdm%maxFO
call messages_info(1)
end if
endif
if (ierr /= 0) then
message(1) = 'Unable to write states wavefunctions.'
call messages_warning(1)
end if
endif
! write output for iterations if requested
if (outp%what/=0 .and. outp%duringscf .and. outp%output_interval /= 0 &
.and. mod(iter, outp%output_interval) == 0) then
write(dirname,'(a,a,i4.4)') trim(outp%iter_dir), "scf.", iter
call output_all(outp, namespace, dirname, gr, geo, st, hm, ks)
call scf_write_static(dirname, "info")
end if
if (conv) exit
end do
if(conv) then
write(message(1),'(a,i3,a)') 'The calculation converged after ',rdm%iter,' iterations'
write(message(2),'(a,9x,es20.10)') 'The total energy is ', units_from_atomic(units_out%energy,energy + hm%ep%eii)
call messages_info(2)
else
write(message(1),'(a,i3,a)') 'The calculation did not converge after ', iter-1, ' iterations '
write(message(2),'(a,es15.5)') 'Relative energy difference between the last two iterations ', rel_ener
write(message(3),'(a,es15.5)') 'The maximal non-diagonal element of the Hermitian matrix F is ', rdm%maxFO
call messages_info(3)
end if
call scf_write_static(STATIC_DIR, "info")
call output_all(outp, namespace, STATIC_DIR, gr, geo, st, hm, ks)
POP_SUB(scf_rdmft)
contains
! ---------------------------------------------------------
subroutine scf_write_static(dir, fname)
character(len=*), intent(in) :: dir, fname
integer :: iunit, ist
FLOAT, allocatable :: photon_number_state (:), ekin_state (:), epot_state (:)
PUSH_SUB(scf_rdmft.scf_write_static)
SAFE_ALLOCATE(photon_number_state(1:st%nst))
SAFE_ALLOCATE(ekin_state(1:st%nst))
SAFE_ALLOCATE(epot_state(1:st%nst))
if(mpi_grp_is_root(mpi_world)) then
call io_mkdir(dir, namespace)
iunit = io_open(trim(dir) // "/" // trim(fname), namespace, action='write')
call grid_write_info(gr, geo, iunit)
call v_ks_write_info(ks, iunit, namespace)
if (rdm%do_basis) then
write(iunit, '(a)')'Orbital optimization with [basis set]'
else
write(iunit, '(a)')'Orbital optimization with [conjugated gradients]'
end if
write(iunit, '(1x)')
if (rdm%hf) then
write(iunit, '(a)')'Hartree Fock calculation'
write(iunit, '(1x)')
end if
if (hm%psolver%is_dressed) then
write(iunit, '(a)')'Dressed state calculation'
call photon_mode_write_info(hm%psolver%photons, iunit)
write(iunit, '(1x)')
end if
! scf information
if(conv) then
write(iunit, '(a, i4, a)')'SCF converged in ', iter, ' iterations'
else
write(iunit, '(a)') 'SCF *not* converged!'
end if
write(iunit, '(1x)')
write(iunit, '(3a,es20.10)') 'Total Energy [', trim(units_abbrev(units_out%energy)), ']:', &
units_from_atomic(units_out%energy, energy + hm%ep%eii)
write(iunit,'(a,1x,f16.12)') 'Sum of occupation numbers:', rdm%occsum
else
iunit = 0
end if
if (hm%psolver%is_dressed) then
call calc_photon_number(gr, st, hm%psolver%photons, photon_number_state, ekin_state, epot_state)
if(mpi_grp_is_root(mpi_world)) then
write(iunit,'(a,1x,f14.12)') 'Total mode occupation:', hm%psolver%photons%number(1)
end if
end if
if(mpi_grp_is_root(mpi_world)) then
if (rdm%max_iter > 0) then
write(iunit, '(a)') 'Convergence:'
write(iunit, '(6x, a, es15.8,a,es15.8,a)') 'maxFO = ', rdm%maxFO
write(iunit, '(6x, a, es15.8,a,es15.8,a)') 'rel_ener = ', rel_ener
write(iunit,'(1x)')
end if
! otherwise, these values are uninitialized, and unknown.
end if
if (mpi_grp_is_root(mpi_world)) then
! Write header
write(iunit,'(a)') 'Natural occupation numbers:'
write(iunit,'(a4,5x,a12)', advance='no') '#st', 'Occupation'
if (.not. rdm%do_basis) write(iunit,'(5x,a12)', advance='no') 'conv'
if (hm%psolver%is_dressed) write(iunit,'(3(5x,a12))', advance='no') 'Mode Occ.', '-1/2d^2/dq^2', '1/2w^2q^2'
write(iunit,*)
! Write values
do ist = 1, st%nst
write(iunit,'(i4,3x,f14.12)', advance='no') ist, st%occ(ist, 1)
if (.not. rdm%do_basis) write(iunit,'(3x,f14.12)', advance='no') rdm%eigens%diff(ist, 1)
if (hm%psolver%is_dressed) then
write(iunit,'(3(3x,f14.12))', advance='no') photon_number_state(ist), ekin_state(ist), epot_state(ist)
end if
write(iunit,*)
end do
end if
if (mpi_grp_is_root(mpi_world)) then
call io_close(iunit)
end if
SAFE_DEALLOCATE_A(photon_number_state)
SAFE_DEALLOCATE_A(ekin_state)
SAFE_DEALLOCATE_A(epot_state)
POP_SUB(scf_rdmft.scf_write_static)
end subroutine scf_write_static
end subroutine scf_rdmft
! ---------------------------------------------------------
subroutine calc_maxFO (namespace, hm, st, gr, rdm)
type(namespace_t), intent(in) :: namespace
type(rdm_t), intent(inout) :: rdm
type(grid_t), intent(in) :: gr
type(hamiltonian_elec_t), intent(inout) :: hm
type(states_elec_t), intent(inout) :: st
FLOAT, allocatable :: lambda(:,:), FO(:,:)
integer :: ist, jst
PUSH_SUB(calc_maxFO)
SAFE_ALLOCATE(lambda(1:st%nst,1:st%nst))
SAFE_ALLOCATE(FO(1:st%nst, 1:st%nst))
! calculate FO operator to check Hermiticity of lagrange multiplier matrix (lambda)
lambda = M_ZERO
FO = M_ZERO
call construct_lambda(namespace, hm, st, gr, lambda, rdm)
!Set up FO matrix to check maxFO
do ist = 1, st%nst
do jst = 1, ist - 1
FO(jst, ist) = - (lambda(jst, ist) - lambda(ist ,jst))
end do
end do
rdm%maxFO = maxval(abs(FO))
SAFE_DEALLOCATE_A(lambda)
SAFE_DEALLOCATE_A(FO)
POP_SUB(calc_maxFO)
end subroutine calc_maxFO
! ---------------------------------------------------------
subroutine calc_photon_number(gr, st, photons, photon_number_state, ekin_state, epot_state)
type(grid_t), intent(in) :: gr
type(states_elec_t), intent(in) :: st
type(photon_mode_t), intent(inout) :: photons
FLOAT, intent(out) :: photon_number_state(:)
FLOAT, intent(out) :: ekin_state(:)
FLOAT, intent(out) :: epot_state(:)
integer :: ist, dim_photon
FLOAT :: q2_exp, laplace_exp
FLOAT, allocatable :: psi(:, :), psi_q2(:), dpsidq(:), d2psidq2(:)
PUSH_SUB(calc_photon_number)
! The photon dimension is always the last
dim_photon = gr%mesh%sb%dim
SAFE_ALLOCATE(psi(1:gr%mesh%np_part, 1))
SAFE_ALLOCATE(psi_q2(1:gr%mesh%np))
SAFE_ALLOCATE(dpsidq(1:gr%mesh%np_part))
SAFE_ALLOCATE(d2psidq2(1:gr%mesh%np))
photons%number(1) = M_ZERO
do ist = 1, st%nst
call states_elec_get_state(st, gr%mesh, ist, 1, psi)
! <phi(ist)|d^2/dq^2|phi(ist)> ~= <phi(ist)| d/dq (d/dq|phi(ist)>)
call dderivatives_partial(gr%der, psi(:, 1), dpsidq(:), dim_photon, ghost_update = .true., set_bc = .true.)
call dderivatives_partial(gr%der, dpsidq(1:gr%mesh%np_part), d2psidq2(:), dim_photon, ghost_update = .true., set_bc = .true.)
laplace_exp = dmf_dotp(gr%mesh, psi(:, 1), d2psidq2(:))
ekin_state(ist) = -M_HALF*laplace_exp
! <phi(ist)|q^2|psi(ist)>= |q|psi(ist)>|^2
psi_q2(1:gr%mesh%np) = psi(1:gr%mesh%np, 1) * gr%mesh%x(1:gr%mesh%np, dim_photon)**2
q2_exp = dmf_dotp(gr%mesh, psi(:, 1), psi_q2(:))
epot_state(ist) = M_HALF * photons%omega(1)**2 * q2_exp
!! N_phot(ist)=( <phi_i|H_ph|phi_i>/omega - 0.5 ) / N_elec
!! 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)>
photon_number_state(ist) = -M_HALF*laplace_exp / photons%omega(1) + M_HALF * photons%omega(1) * q2_exp
photon_number_state(ist) = photon_number_state(ist) - M_HALF
!! N_phot_total= sum_ist occ_ist*N_phot(ist)
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
end do
photons%number(1) = photons%number(1) - st%qtot/M_TWO
SAFE_DEALLOCATE_A(psi)
SAFE_DEALLOCATE_A(psi_q2)
SAFE_DEALLOCATE_A(dpsidq)
SAFE_DEALLOCATE_A(d2psidq2)
POP_SUB(calc_photon_number)
end subroutine calc_photon_number
! ---------------------------------------------------------
! reset occ.num. to 2/0
subroutine set_occ_pinning(st)
type(states_elec_t), intent(inout) :: st
FLOAT, allocatable :: occin(:,:)
PUSH_SUB(set_occ_pinning)
SAFE_ALLOCATE(occin(1:st%nst, 1:st%d%nik))
occin(1:st%nst, 1:st%d%nik) = st%occ(1:st%nst, 1:st%d%nik)
where(occin(:,:) < M_ONE) occin(:,:) = M_ZERO
where(occin(:,:) > M_ONE) occin(:,:) = st%smear%el_per_state
st%occ = occin
SAFE_DEALLOCATE_A(occin)
POP_SUB(set_occ_pinning)
end subroutine set_occ_pinning
! ---------------------------------------------------------
! dummy routine for occupation numbers which only calculates the necessary variables for further use
! used in Hartree-Fock mode
subroutine scf_occ_NO(rdm, namespace, gr, hm, st, energy)
type(rdm_t), intent(inout) :: rdm
type(namespace_t), intent(in) :: namespace
type(grid_t), intent(in) :: gr
type(hamiltonian_elec_t), intent(in) :: hm
type(states_elec_t), intent(inout) :: st
FLOAT, intent(out) :: energy
integer :: ist
PUSH_SUB(scf_occ_NO)
write(message(1),'(a)') 'SKIP Optimization of occupation numbers'
call messages_info(1)
call set_occ_pinning(st)
energy = M_ZERO
call rdm_derivatives(rdm, namespace, hm, st, gr)
call total_energy_rdm(rdm, st%occ(:,1), energy)
rdm%occsum = sum(st%occ(1:st%nst, 1:st%d%nik))
write(message(1),'(a4,5x,a12)')'#st','Occupation'
call messages_info(1)
do ist = 1, st%nst
write(message(1),'(i4,3x,f11.9)') ist, st%occ(ist, 1)
call messages_info(1)
end do
write(message(1),'(a,1x,f13.9)') 'Sum of occupation numbers', rdm%occsum
write(message(2),'(a,es20.10)') 'Total energy occ', units_from_atomic(units_out%energy,energy + hm%ep%eii)
call messages_info(2)
POP_SUB(scf_occ_NO)
end subroutine scf_occ_NO
! scf for the occupation numbers
subroutine scf_occ(rdm, namespace, gr, hm, st, energy)
type(rdm_t), target, intent(inout) :: rdm
type(namespace_t), intent(in) :: namespace
type(grid_t), intent(in) :: gr
type(hamiltonian_elec_t), intent(in) :: hm
type(states_elec_t), intent(inout) :: st
FLOAT, intent(out) :: energy
integer :: ist, icycle, ierr
FLOAT :: sumgi1, sumgi2, sumgim, mu1, mu2, mum, dinterv, thresh_occ
FLOAT, allocatable :: occin(:,:)
FLOAT, parameter :: smallocc = CNST(0.00001)
FLOAT, allocatable :: theta(:)
FLOAT :: objective
type(profile_t), save :: prof_occ
PUSH_SUB(scf_occ)
call profiling_in(prof_occ, "SCF_OCC")
write(message(1),'(a)') 'Optimization of occupation numbers'
call messages_info(1)
SAFE_ALLOCATE(occin(1:st%nst, 1:st%d%nik))
SAFE_ALLOCATE(theta(1:st%nst))
occin = M_ZERO
theta = M_ZERO
energy = M_ZERO
! Defines a threshold on occ nums to avoid numerical instabilities.
! Needs to be changed consistently with the same variable in objective_rdmft
thresh_occ = CNST(1e-14)
!Initialize the occin. Smallocc is used for numerical stability
occin(1:st%nst, 1:st%d%nik) = st%occ(1:st%nst, 1:st%d%nik)
where(occin(:,:) < smallocc) occin(:,:) = smallocc
where(occin(:,:) > st%smear%el_per_state - smallocc) occin(:,:) = st%smear%el_per_state - smallocc
!Renormalize the occupation numbers
rdm%occsum = st%qtot
st%occ = occin
call rdm_derivatives(rdm, namespace, hm, st, gr)
!finding the chemical potential mu such that the occupation numbers sum up to the number of electrons
!bisection to find the root of rdm%occsum-st%qtot=M_ZERO
mu1 = rdm%mu !initial guess for mu
mu2 = -CNST(1.0e-6)
dinterv = M_HALF
! Set pointer to rdm, so that it is available in the functions called by the minimizer
rdm_ptr => rdm
!use n_j=sin^2(2pi*theta_j) to treat pinned states, minimize for both intial mu
theta(:) = asin(sqrt(occin(:, 1)/st%smear%el_per_state))*(M_HALF/M_PI)
call minimize_multidim(MINMETHOD_BFGS, st%nst, theta, CNST(0.05), CNST(0.01), &
CNST(1e-12), CNST(1e-12), 200, objective_rdmft, write_iter_info_rdmft, objective, ierr)
sumgi1 = rdm%occsum - st%qtot
rdm%mu = mu2
theta(:) = asin(sqrt(occin(:, 1)/st%smear%el_per_state))*(M_HALF/M_PI)
call minimize_multidim(MINMETHOD_BFGS, st%nst, theta, CNST(0.05), CNST(0.01), &
CNST(1e-12), CNST(1e-12), 200, objective_rdmft, write_iter_info_rdmft, objective, ierr)
sumgi2 = rdm%occsum - st%qtot
! Adjust the interval between the initial mu to include the root of rdm%occsum-st%qtot=M_ZERO
do while (sumgi1*sumgi2 > M_ZERO)
if (sumgi2 > M_ZERO) then
mu2 = mu1
sumgi2 = sumgi1
mu1 = mu1 - dinterv
rdm%mu = mu1
theta(:) = asin(sqrt(occin(:, 1)/st%smear%el_per_state))*(M_HALF/M_PI)
call minimize_multidim(MINMETHOD_BFGS, st%nst, theta, CNST(0.05), CNST(0.01), &
CNST(1e-12), CNST(1e-12), 200, objective_rdmft, write_iter_info_rdmft, objective, ierr)
sumgi1 = rdm%occsum - st%qtot
else
mu1 = mu2
sumgi1 = sumgi2
mu2 = mu2 + dinterv
rdm%mu = mu2
theta(:) = asin(sqrt(occin(:, 1)/st%smear%el_per_state))*(M_HALF/M_PI)
call minimize_multidim(MINMETHOD_BFGS, st%nst, theta, CNST(0.05), CNST(0.01), &
CNST(1e-12), CNST(1e-12), 200, objective_rdmft, write_iter_info_rdmft, objective, ierr)
sumgi2 = rdm%occsum - st%qtot
end if
end do
do icycle = 1, 50
mum = (mu1 + mu2)*M_HALF
rdm%mu = mum
theta(:) = asin(sqrt(occin(:, 1)/st%smear%el_per_state))*(M_HALF/M_PI)
call minimize_multidim(MINMETHOD_BFGS, st%nst, theta, CNST(0.05), CNST(0.0001), &
CNST(1e-12), CNST(1e-12), 200, objective_rdmft, write_iter_info_rdmft, objective, ierr)
sumgim = rdm%occsum - st%qtot
if (sumgi1*sumgim < M_ZERO) then
mu2 = mum
else
mu1 = mum
sumgi1 = sumgim
end if
! check occ.num. threshold again after minimization
do ist = 1, st%nst
st%occ(ist,1) = M_TWO*sin(theta(ist)*M_PI*M_TWO)**2
if (st%occ(ist,1) <= thresh_occ ) st%occ(ist,1) = thresh_occ
end do
if (abs(sumgim) < rdm%toler .or. abs((mu1-mu2)*M_HALF) < rdm%toler) exit
end do
nullify(rdm_ptr)
if (icycle >= 50) then
write(message(1),'(a,1x,f11.4)') 'Bisection ended without finding mu, sum of occupation numbers:', rdm%occsum
call messages_fatal(1)
end if
do ist = 1, st%nst
st%occ(ist, 1) = st%smear%el_per_state*sin(theta(ist)*M_PI*M_TWO)**2
end do
objective = objective + rdm%mu*(rdm%occsum - rdm%qtot)
energy = objective
write(message(1),'(a4,5x,a12)')'#st','Occupation'
call messages_info(1)
do ist = 1, st%nst
write(message(1),'(i4,3x,f14.12)') ist, st%occ(ist, 1)
call messages_info(1)
end do
write(message(1),'(a,3x,f11.9)') 'Sum of occupation numbers: ', rdm%occsum
write(message(2),'(a,11x,es20.10)') 'Total energy: ', units_from_atomic(units_out%energy, energy + hm%ep%eii)
call messages_info(2)
SAFE_DEALLOCATE_A(occin)
SAFE_DEALLOCATE_A(theta)
call profiling_out(prof_occ)
POP_SUB(scf_occ)
end subroutine scf_occ
! ---------------------------------------------------------
subroutine objective_rdmft(size, theta, objective, getgrad, df)
integer, intent(in) :: size
REAL_DOUBLE, intent(in) :: theta(size)
REAL_DOUBLE, intent(inout) :: objective
integer, intent(in) :: getgrad
REAL_DOUBLE, intent(inout) :: df(size)
integer :: ist
FLOAT :: thresh_occ, thresh_theta
FLOAT, allocatable :: dE_dn(:),occ(:)
PUSH_SUB(objective_rdmft)
ASSERT(size == rdm_ptr%nst)
SAFE_ALLOCATE(dE_dn(1:size))
SAFE_ALLOCATE(occ(1:size))
occ = M_ZERO
! Defines a threshold on occ nums to avoid numerical instabilities.
! Needs to be changed consistently with the same variable in scf_occ
thresh_occ = CNST(1e-14)
thresh_theta = asin(sqrt(thresh_occ/M_TWO))*(M_HALF/M_PI)
do ist = 1, size
occ(ist) = M_TWO*sin(theta(ist)*M_PI*M_TWO)**2
if (occ(ist) <= thresh_occ ) occ(ist) = thresh_occ
end do
rdm_ptr%occsum = sum(occ(1:size))
!calculate the total energy without nuclei interaction and the energy
!derivatives with respect to the occupation numbers
call total_energy_rdm(rdm_ptr, occ, objective, dE_dn)
do ist = 1, size
if (occ(ist) <= thresh_occ ) then
df(ist) = M_FOUR*M_PI*sin(M_FOUR*thresh_theta*M_PI)*(dE_dn(ist) - rdm_ptr%mu)
else
df(ist) = M_FOUR*M_PI*sin(M_FOUR*theta(ist)*M_PI)*(dE_dn(ist) - rdm_ptr%mu)
end if
end do
objective = objective - rdm_ptr%mu*(rdm_ptr%occsum - rdm_ptr%qtot)
SAFE_DEALLOCATE_A(dE_dn)
SAFE_DEALLOCATE_A(occ)
POP_SUB(objective_rdmft)
end subroutine objective_rdmft
! ---------------------------------------------------------
subroutine write_iter_info_rdmft(iter, size, energy, maxdr, maxdf, theta)
integer, intent(in) :: iter
integer, intent(in) :: size
FLOAT, intent(in) :: energy, maxdr, maxdf
FLOAT, intent(in) :: theta(size)
PUSH_SUB(write_iter_info_rdmft)
! Nothing to do.
POP_SUB(write_iter_info_rdmft)
end subroutine write_iter_info_rdmft
! scf for the natural orbitals
subroutine scf_orb(rdm, namespace, gr, st, hm, energy)
type(rdm_t), intent(inout) :: rdm
type(namespace_t), intent(in) :: namespace
type(grid_t), intent(in) :: gr !< grid
type(states_elec_t), intent(inout) :: st !< States
type(hamiltonian_elec_t), intent(in) :: hm !< Hamiltonian
FLOAT, intent(out) :: energy
integer :: ist, jst
FLOAT, allocatable :: lambda(:,:), fo(:,:)
type(profile_t), save :: prof_orb_basis
PUSH_SUB(scf_orb)
call profiling_in(prof_orb_basis, "SCF_ORB_BASIS")
!matrix of Lagrange Multipliers from Equation (8), Piris and Ugalde, Vol. 30, No. 13, J. Comput. Chem.
SAFE_ALLOCATE(lambda(1:st%nst,1:st%nst))
SAFE_ALLOCATE(fo(1:st%nst, 1:st%nst)) !Generalized Fockian Equation (11)
lambda = M_ZERO
fo = M_ZERO
call construct_lambda(namespace, hm, st, gr, lambda, rdm)
!Set up fo matrix
if (rdm%iter==1) then
do ist = 1, st%nst
do jst = 1, ist
fo(ist, jst) = M_HALF*(lambda(ist, jst) + lambda(jst, ist))
fo(jst, ist) = fo(ist, jst)
end do
end do
else
do ist = 1, st%nst
do jst = 1, ist - 1
fo(jst, ist) = - ( lambda(jst, ist) - lambda(ist ,jst))
end do
end do
rdm%maxfo = maxval(abs(fo))
do ist = 1, st%nst
fo(ist, ist) = rdm%evalues(ist)
do jst = 1, ist-1
if(abs(fo(jst, ist)) > rdm%scale_f) then
fo(jst, ist) = rdm%scale_f*fo(jst,ist)/abs(fo(jst, ist))
end if
fo(ist, jst) = fo(jst, ist)
end do
end do
end if
call lalg_eigensolve(st%nst, fo, rdm%evalues)
call assign_eigfunctions(rdm, st, fo)