/
m_scfcv_core.F90
3275 lines (2917 loc) · 136 KB
/
m_scfcv_core.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
!{\src2tex{textfont=tt}}
!!****m* ABINIT/m_scfcv_core
!! NAME
!! m_scfcv_core
!!
!! FUNCTION
!!
!!
!! COPYRIGHT
!! Copyright (C) 1998-2018 ABINIT group (XG, GMR, AR, MKV, MT, FJ, MB)
!! This file is distributed under the terms of the
!! GNU General Public License, see ~abinit/COPYING
!! or http://www.gnu.org/copyleft/gpl.txt .
!!
!! PARENTS
!!
!! CHILDREN
!!
!! SOURCE
#if defined HAVE_CONFIG_H
#include "config.h"
#endif
#include "abi_common.h"
module m_scfcv_core
use defs_basis
use defs_datatypes
use defs_abitypes
use defs_wvltypes
use defs_rectypes
use m_xmpi
use m_abicore
use m_wffile
use m_rec
use m_ab7_mixing
use m_errors
use m_efield
use mod_prc_memory
use m_nctk
use m_hdr
use m_xcdata
use m_cgtools
use m_berryphase_new, only : update_e_field_vars
use m_dtfil, only : status
use m_time, only : timab
use m_fstrings, only : int2char4, sjoin
use m_symtk, only : symmetrize_xred
use m_geometry, only : metric
use m_fftcore, only : getng, sphereboundary
use m_time, only : abi_wtime, sec2str
use m_exit, only : get_start_time, have_timelimit_in, get_timelimit, enable_timelimit_in
use m_mpinfo, only : destroy_mpi_enreg, iwrite_fftdatar, initmpi_seq, proc_distrb_cycle
use m_ioarr, only : fftdatar_write_from_hdr
use m_results_gs , only : results_gs_type
use m_scf_history, only : scf_history_type, scf_history_init, scf_history_free
use m_energies, only : energies_type, energies_init, energies_copy
use m_electronpositron, only : electronpositron_type, electronpositron_calctype
use m_pawang, only : pawang_type
use m_pawrad, only : pawrad_type
use m_pawtab, only : pawtab_type,pawtab_get_lsize
use m_paw_an, only : paw_an_type, paw_an_init, paw_an_free, paw_an_nullify, paw_an_reset_flags
use m_pawfgrtab, only : pawfgrtab_type, pawfgrtab_init, pawfgrtab_free
use m_pawrhoij, only : pawrhoij_type
use m_pawcprj, only : pawcprj_type, pawcprj_alloc, pawcprj_copy, pawcprj_get, pawcprj_lincom, &
& pawcprj_free, pawcprj_axpby, pawcprj_put, pawcprj_getdim, pawcprj_reorder
use m_pawdij, only : pawdij, symdij,pawdijhat
use m_pawfgr, only : pawfgr_type
use m_paw_ij, only : paw_ij_type, paw_ij_init, paw_ij_free, paw_ij_nullify, paw_ij_reset_flags
use m_paw_dmft, only : paw_dmft_type
use m_paw_nhat, only : nhatgrid,wvl_nhatgrid,pawmknhat
use m_paw_tools, only : chkpawovlp
use m_paw_denpot, only : pawdenpot
use m_paw_occupancies, only : pawmkrhoij
use m_paw_correlations, only : setnoccmmp,setrhoijpbe0
use m_orbmag, only : orbmag_type
use m_paw_mkrho, only : pawmkrho
use m_paw_uj, only : pawuj_red
use m_paw_dfpt, only : pawgrnl
use m_fock, only : fock_type, fock_init, fock_destroy, fock_ACE_destroy, fock_common_destroy, &
fock_BZ_destroy, fock_update_exc, fock_updatecwaveocc
use m_gwls_hamiltonian, only : build_vxc
#if defined HAVE_BIGDFT
use BigDFT_API, only : cprj_clean,cprj_paw_alloc
#endif
use m_io_kss, only : gshgg_mkncwrite
use m_outxml, only : out_resultsgs_XML, out_geometry_XML
use m_kg, only : getcut, getmpw, kpgio, getph
use m_fft, only : fourdp
use m_vtorhorec, only : first_rec, vtorhorec
use m_vtorhotf, only : vtorhotf
use m_outscfcv, only : outscfcv
use m_afterscfloop, only : afterscfloop
use m_extraprho, only : extraprho
use m_spacepar, only : setsym
use m_newrho, only : newrho
use m_newvtr, only : newvtr
use m_vtorho, only : vtorho
use m_setvtr, only : setvtr
use m_mkrho, only : mkrho
use m_rhotov, only : rhotov
use m_forces, only : fresid, forces
use m_dft_energy, only : energy
use m_initylmg, only : initylmg
use m_rhotoxc, only : rhotoxc
use m_drivexc, only : check_kxc
use m_odamix, only : odamix
use m_common, only : scprqt, prtene
use m_fourier_interpol, only : transgrid
use m_fock_getghc, only : fock2ACE
use m_forstr, only : nres2vres
use m_positron, only : setup_positron
use m_cgprj, only : ctocprj
use m_psolver, only : psolver_rhohxc
use m_paw2wvl, only : paw2wvl_ij, wvl_cprjreorder
implicit none
private
!!***
public :: scfcv_core
!!***
contains
!!***
!!****f* ABINIT/scfcv_core
!! NAME
!! scfcv_core
!!
!! FUNCTION
!! Self-consistent-field convergence.
!! Conducts set of passes or overall iterations of preconditioned
!! conjugate gradient algorithm to converge wavefunctions to
!! ground state and optionally to compute forces and energy.
!! This routine is called to compute forces for given atomic
!! positions or else to do non-SCF band structures.
!!
!! INPUTS
!! atindx(natom)=index table for atoms (see gstate.f)
!! atindx1(natom)=index table for atoms, inverse of atindx (see gstate.f)
!! cpus= cpu time limit in seconds
!! dmatpawu= fixed occupation matrix of correlated orbitals (DFT+U or DMFT only)
!! dtfil <type(datafiles_type)>=variables related to files
!! dtset <type(dataset_type)>=all input variables for this dataset
!! | mband=maximum number of bands
!! | mgfft=maximum size of 1D FFTs for the "coarse" grid (see NOTES below)
!! | mkmem =number of k points treated by this node.
!! | mpw=maximum dimensioned size of npw.
!! | natom=number of atoms in cell.
!! | nfft=(effective) number of FFT grid points (for this processor)
!! | for the "coarse" grid (see NOTES below)
!! | nkpt=number of k points
!! | nspden=number of spin-density components
!! | nsppol=1 for unpolarized, 2 for spin-polarized
!! | nsym=number of symmetry elements in space group
!! ecore=core psp energy (part of total energy) (hartree)
!! fatvshift=factor to multiply dtset%atvshift
!! kg(3,mpw*mkmem)=reduced planewave coordinates.
!! mcg=size of wave-functions array (cg) =mpw*my_nspinor*mband*mkmem*nsppol
!! mpi_enreg=information about MPI parallelization
!! my_natom=number of atoms treated by current processor
!! nattyp(ntypat)= # atoms of each type.
!! ndtpawuj=size of dtpawuj
!! npwarr(nkpt)=number of planewaves in basis at this k point
!! paw_dmft <type(paw_dmft_type)>= paw+dmft related data
!! pawang <type(pawang_type)>=paw angular mesh and related data
!! pawfgr <type(pawfgr_type)>=fine grid parameters and related data
!! pawrad(ntypat*usepaw) <type(pawrad_type)>=paw radial mesh and
!! related data
!! pawtab(ntypat*usepaw) <type(pawtab_type)>=paw tabulated starting data
!! psps <type(pseudopotential_type)>=variables related to pseudopotentials
!! | mpsang= 1+maximum angular momentum for nonlocal pseudopotentials
!! pwind(pwind_alloc,2,3) = array used to compute
!! the overlap matrix smat between k-points (see initberry.f)
!! pwind_alloc = first dimension of pwind
!! pwnsfac(2,pwind_alloc) = phase factors for non-symmorphic translations
!! (see initberry.f)
!! ylm(mpw*mkmem,mpsang*mpsang*useylm)= real spherical harmonics for
!! each G and k point
!! ylmgr(mpw*mkmem,3,mpsang*mpsang*useylm)= gradients of real
!! spherical harmonics
!!
!! OUTPUT
!! resid(mband*nkpt*nsppol)=residuals for each band over all k points and spins
!!
!! SIDE EFFECTS
!! cg(2,mcg)=updated wavefunctions; if mkmem>=nkpt, these are kept in a disk file.
!! dtefield <type(efield_type)> = variables related to Berry phase
!! dtorbmag <type(orbmag_type)> = variables related to orbital magnetization
!! dtpawuj(ndtpawuj)= data used for the automatic determination of U
!! (relevant only for PAW+U) calculations (see initberry.f)
!! eigen(mband*nkpt*nsppol)=array for holding eigenvalues (hartree)
!! electronpositron <type(electronpositron_type)>=quantities for
!! the electron-positron annihilation
!! hdr <type(hdr_type)>=the header of wf, den and pot files
!! indsym(4,nsym,natom)=indirect indexing array for atom labels
!! initialized= if 0 the initialization of the gstate run is not yet finished
!! irrzon(nfft**(1-1/nsym),2,(nspden/nsppol)-3*(nspden/4))=irreducible zone data
!! nfftf=(effective) number of FFT grid points (for this processor)
!! for the "fine" grid (see NOTES below)
!! occ(mband*nkpt*nsppol)=occupation number for each band (often 2) at each k point
!! pawrhoij(my_natom*usepaw) <type(pawrhoij_type)>= -PAW only- atomic occupancies
!! phnons(2,nfft**(1-1/nsym),(nspden/nsppol)-3*(nspden/4))=nonsymmorphic translation phases
!! results_gs <type(results_gs_type)>=results (energy and its components,
!! forces and its components, the stress tensor) of a ground-state
!! computation (should be made a pure output quantity)
!! rhog(2,nfftf)=array for Fourier transform of electron density
!! rhor(nfftf,nspden)=array for electron density in el./bohr**3
!! rprimd(3,3)=dimensional primitive translations in real space (bohr)
!! scf_history <type(scf_history_type)>=arrays obtained from previous
!! SCF cycles
!! symrec(3,3,nsym)=symmetry operations in reciprocal space
!! taug(2,nfftf*dtset%usekden)=array for Fourier transform of kinetic
!! energy density
!! taur(nfftf,nspden*dtset%usekden)=array for kinetic energy density
!! wffnew=struct info for wf disk files
!! wvl <type(wvl_data)>=all wavelets data
!! xred(3,natom)=reduced dimensionless atomic coordinates
!! xred_old(3,natom)= at input, previous reduced dimensionless atomic
!! coordinates at output, current xred is transferred to xred_old
!! conv_retcode=return code, 0 if convergence was achieved
!!
!! NOTES
!! It is worth to explain THE USE OF FFT GRIDS:
!! ============================================
!! In case of PAW:
!! ---------------
!! Two FFT grids are used:
!! - A "coarse" FFT grid (defined by ecut)
!! for the application of the Hamiltonian on the plane waves basis.
!! It is defined by nfft, ngfft, mgfft, ...
!! Hamiltonian, wave-functions, density related to WFs (rhor here), ...
!! are expressed on this grid.
!! - A "fine" FFT grid (defined) by ecutdg)
!! for the computation of the density inside PAW spheres.
!! It is defined by nfftf, ngfftf, mgfftf, ...
!! Total density, potentials, ...
!! are expressed on this grid.
!! In case of norm-conserving:
!! ---------------------------
!! - Only the usual FFT grid (defined by ecut) is used.
!! It is defined by nfft, ngfft, mgfft, ...
!! For compatibility reasons, (nfftf,ngfftf,mgfftf)
!! are set equal to (nfft,ngfft,mgfft) in that case.
!!
!! PARENTS
!! m_scfcv
!!
!! CHILDREN
!! ab7_mixing_deallocate,ab7_mixing_new,ab7_mixing_use_disk_cache
!! afterscfloop,build_vxc,check_kxc,chkpawovlp,cprj_clean,cprj_paw_alloc
!! ctocprj,destroy_distribfft,destroy_mpi_enreg,energies_init,energy
!! etotfor,extraprho,fftdatar_write_from_hdr,first_rec,fock2ace
!! fock_ace_destroy,fock_bz_destroy,fock_common_destroy,fock_destroy
!! fock_init,fock_updatecwaveocc,fourdp,fresid,getcut,getmpw,getng,getph
!! gshgg_mkncwrite,hdr_update,init_distribfft,init_distribfft_seq
!! init_metricrec,initmpi_seq,initylmg,int2char4,kpgio,metric,mkrho,newrho
!! newvtr,nhatgrid,odamix,out_geometry_xml,out_resultsgs_xml,outscfcv
!! paw2wvl_ij,paw_an_free,paw_an_init,paw_an_nullify,paw_an_reset_flags
!! paw_ij_free,paw_ij_init,paw_ij_nullify,paw_ij_reset_flags,pawcprj_alloc
!! pawcprj_free,pawcprj_getdim,pawcprj_reorder,pawdenpot,pawdij
!! pawfgrtab_free,pawfgrtab_init,pawmknhat,pawmkrho,pawmkrhoij
!! pawtab_get_lsize,pawuj_red,prc_mem_free,prtene,psolver_rhohxc,rhotov
!! rhotoxc,scf_history_free,scf_history_init,scprqt,setnoccmmp
!! setrhoijpbe0,setsym,setup_positron,setvtr,sphereboundary,status,symdij
!! symmetrize_xred,timab,transgrid,update_e_field_vars,vtorho,vtorhorec
!! vtorhotf,wf_mixing,wrtout,wvl_cprjreorder,wvl_nhatgrid,xcdata_init
!! xmpi_isum,xmpi_sum,xmpi_wait
!!
!! SOURCE
subroutine scfcv_core(atindx,atindx1,cg,cprj,cpus,dmatpawu,dtefield,dtfil,dtorbmag,dtpawuj,&
& dtset,ecore,eigen,electronpositron,fatvshift,hdr,indsym,&
& initialized,irrzon,kg,mcg,mcprj,mpi_enreg,my_natom,nattyp,ndtpawuj,nfftf,npwarr,occ,&
& paw_dmft,pawang,pawfgr,pawrad,pawrhoij,pawtab,phnons,psps,pwind,&
& pwind_alloc,pwnsfac,rec_set,resid,results_gs,rhog,rhor,rprimd,&
& scf_history,symrec,taug,taur,wffnew,wvl,xred,xred_old,ylm,ylmgr,conv_retcode)
!This section has been created automatically by the script Abilint (TD).
!Do not modify the following lines by hand.
#undef ABI_FUNC
#define ABI_FUNC 'scfcv_core'
!End of the abilint section
implicit none
!Arguments ------------------------------------
!scalars
integer,intent(in) :: mcg,my_natom,ndtpawuj,pwind_alloc
integer,intent(inout) :: initialized,nfftf,mcprj
integer,intent(out) :: conv_retcode
real(dp),intent(in) :: cpus,ecore,fatvshift
type(MPI_type),intent(inout) :: mpi_enreg
type(datafiles_type),intent(in) :: dtfil
type(dataset_type),intent(inout) :: dtset
type(efield_type),intent(inout) :: dtefield
type(orbmag_type),intent(inout) :: dtorbmag
type(electronpositron_type),pointer:: electronpositron
type(hdr_type),intent(inout) :: hdr
type(pawang_type),intent(in) :: pawang
type(pawfgr_type),intent(inout) :: pawfgr
type(pseudopotential_type),intent(in) :: psps
type(recursion_type),intent(inout) :: rec_set
type(results_gs_type),intent(inout) :: results_gs
type(scf_history_type),intent(inout) :: scf_history
type(wffile_type),intent(inout) :: wffnew
type(wvl_data),intent(inout) :: wvl
!arrays
integer,intent(in) :: atindx(dtset%natom),atindx1(dtset%natom)
integer,intent(in) :: indsym(4,dtset%nsym,dtset%natom)
!no_abirules
integer, intent(in) :: irrzon(dtset%nfft**(1-1/dtset%nsym),2,(dtset%nspden/dtset%nsppol)-3*(dtset%nspden/4))
!(nfft**(1-1/nsym) is 1 if nsym==1, and nfft otherwise)
integer, intent(in) :: kg(3,dtset%mpw*dtset%mkmem)
integer, intent(in) :: nattyp(psps%ntypat),npwarr(dtset%nkpt),pwind(pwind_alloc,2,3)
integer, intent(in) :: symrec(3,3,dtset%nsym)
real(dp), intent(inout) :: cg(2,mcg),dmatpawu(:,:,:,:)
real(dp), intent(inout) :: eigen(dtset%mband*dtset%nkpt*dtset%nsppol)
real(dp), intent(inout) :: occ(dtset%mband*dtset%nkpt*dtset%nsppol)
real(dp), intent(in) :: phnons(2,dtset%nfft**(1-1/dtset%nsym),(dtset%nspden/dtset%nsppol)-3*(dtset%nspden/4))
!(nfft**(1-1/nsym) is 1 if nsym==1, and nfft otherwise)
real(dp), intent(in) :: pwnsfac(2,pwind_alloc)
real(dp), intent(in) :: rprimd(3,3)
real(dp), pointer :: rhog(:,:),rhor(:,:)
real(dp), pointer :: taug(:,:),taur(:,:)
real(dp), intent(inout) :: resid(dtset%mband*dtset%nkpt*dtset%nsppol)
real(dp), intent(inout) :: xred(3,dtset%natom)
real(dp), intent(inout) :: xred_old(3,dtset%natom)
real(dp), intent(in) :: ylm(dtset%mpw*dtset%mkmem,psps%mpsang*psps%mpsang*psps%useylm)
real(dp), intent(in) :: ylmgr(dtset%mpw*dtset%mkmem,3,psps%mpsang*psps%mpsang*psps%useylm)
type(macro_uj_type),intent(inout) :: dtpawuj(0:ndtpawuj)
type(pawrhoij_type), intent(inout) :: pawrhoij(my_natom*psps%usepaw)
type(pawrad_type), intent(in) :: pawrad(psps%ntypat*psps%usepaw)
type(pawtab_type), intent(inout) :: pawtab(psps%ntypat*psps%usepaw)
type(paw_dmft_type), intent(inout) :: paw_dmft
type(pawcprj_type),pointer, intent(inout) :: cprj(:,:)
!Local variables -------------------------
!scalars
integer,parameter :: level=110,response=0,cplex1=1
integer :: afford,bantot,choice
integer :: computed_forces,cplex,cplex_hf,ctocprj_choice,dbl_nnsclo,dielop,dielstrt,dimdmat
integer :: forces_needed,errid,has_dijhat,has_dijnd,has_dijU,has_vhartree,has_dijfock,history_size,usefock
!integer :: dtset_iprcel
integer :: iatom,ider,idir,ierr,iexit,ii,ikpt,impose_dmat,denpot
integer :: initialized0,iorder_cprj,ipert,ipositron,isave_den,isave_kden,iscf10,ispden
integer :: ispmix,istep,istep_fock_outer,istep_mix,istep_updatedfock,itypat,izero,lmax_diel,lpawumax,mcprj_wvl,mband_cprj
integer :: me,me_wvl,mgfftdiel,mgfftf,moved_atm_inside,moved_rhor,my_nspinor,n1xccc
integer :: n3xccc,ncpgr,nfftdiel,nfftmix,nfftmix_per_nfft,nfftotf,ngrvdw,nhatgrdim,nk3xc,nkxc
integer :: npawmix,npwdiel,nstep,nzlmopt,optcut,optcut_hf,optene,optgr0,optgr0_hf
integer :: optgr1,optgr2,optgr1_hf,optgr2_hf,option,optrad,optrad_hf,optres,optxc,prtfor,prtxml,quit
integer :: quit_sum,rdwrpaw,shft,spaceComm,spaceComm_fft,spaceComm_wvl,spaceComm_grid
integer :: spare_mem
integer :: stress_needed,sz1,sz2,tim_mkrho,unit_out
integer :: usecprj,usexcnhat,use_hybcomp
integer :: my_quit,quitsum_request,timelimit_exit,usecg,wfmixalg
integer ABI_ASYNC :: quitsum_async
real(dp) :: boxcut,compch_fft,compch_sph,deltae,diecut,diffor,ecut
real(dp) :: ecutf,ecutsus,edum,elast,etotal,evxc,fermie,gsqcut,hyb_mixing,hyb_mixing_sr
real(dp) :: maxfor,res2,residm,ucvol,ucvol_local,val_max
real(dp) :: val_min,vxcavg,vxcavg_dum
real(dp) :: zion,wtime_step,now,prev
character(len=10) :: tag
character(len=1500) :: message
!character(len=500) :: dilatmx_errmsg
character(len=fnlen) :: fildata
type(MPI_type) :: mpi_enreg_diel
type(xcdata_type) :: xcdata
type(energies_type) :: energies
type(ab7_mixing_object) :: mix
logical,parameter :: VERBOSE=.FALSE.
logical :: dummy_nhatgr
logical :: finite_efield_flag=.false.
logical :: recompute_cprj=.false.,reset_mixing=.false.
logical,save :: tfw_activated=.false.
logical :: wvlbigdft=.false.
logical :: non_magnetic_xc
!type(energies_type),pointer :: energies_wvl ! TO BE ACTIVATED LATER
!arrays
integer :: ngfft(18),ngfftdiel(18),ngfftf(18),ngfftmix(18),npwarr_diel(1)
integer :: npwtot_diel(1)
integer, save :: scfcv_jdtset = 0 ! To simulate iapp behavior
integer, save :: scfcv_itime = 1 ! To simulate iapp behavior
integer,allocatable :: dimcprj(:),dimcprj_srt(:)
integer,allocatable :: gbound_diel(:,:),irrzondiel(:,:,:),kg_diel(:,:)
integer,allocatable :: l_size_atm(:)
integer,allocatable :: indsym_dum(:,:,:),symrec_dum(:,:,:)
logical,pointer :: lmselect_ep(:,:)
real(dp) :: dielar(7),dphase(3),dummy2(6),favg(3),gmet(3,3),gprimd(3,3)
real(dp) :: kpt_diel(3),pel(3),pel_cg(3),pelev(3),pion(3),ptot(3),qpt(3),red_ptot(3) !!REC
real(dp) :: rhodum(1),rmet(3,3),strsxc(6),strten(6),tollist(12)
real(dp) :: tsec(2),vnew_mean(dtset%nspden),vres_mean(dtset%nspden)
real(dp) :: efield_old_cart(3), ptot_cart(3)
real(dp) :: red_efield2(3),red_efield2_old(3)
real(dp) :: vpotzero(2)
! red_efield1(3),red_efield2(3) is reduced electric field, defined by Eq.(25) of Nat. Phys. suppl. (2009) [[cite:Stengel2009]]
! red_efield1(3) for fixed ebar calculation, red_efield2(3) for fixed reduced d calculation, in mixed BC
! red_efieldbar_lc(3) is local reduced electric field, defined by Eq.(28) of Nat. Phys. suppl. (2009) [[cite:Stengel2009]]
! pbar(3) and dbar(3) are reduced polarization and displacement field,
! defined by Eq.(27) and (29) Nat. Phys. suppl. (2009) [[cite:Stengel2009]]
real(dp),parameter :: k0(3)=(/zero,zero,zero/)
real(dp),allocatable :: dielinv(:,:,:,:,:),dtn_pc(:,:)
real(dp),allocatable :: fcart(:,:),forold(:,:),fred(:,:),gresid(:,:)
real(dp),allocatable :: grchempottn(:,:),grewtn(:,:),grhf(:,:),grnl(:),grvdw(:,:),grxc(:,:)
real(dp),allocatable :: kxc(:,:),nhat(:,:),nhatgr(:,:,:),nvresid(:,:)
real(dp),allocatable :: ph1d(:,:),ph1ddiel(:,:),ph1df(:,:)
real(dp),allocatable :: phnonsdiel(:,:,:),rhowfg(:,:),rhowfr(:,:),shiftvector(:)
real(dp),allocatable :: susmat(:,:,:,:,:),synlgr(:,:)
real(dp),allocatable :: vhartr(:),vpsp(:),vtrial(:,:)
real(dp),allocatable :: vxc(:,:),vxc_hybcomp(:,:),vxctau(:,:,:),workr(:,:),xccc3d(:),ylmdiel(:,:)
real(dp),pointer :: elfr(:,:),grhor(:,:,:),lrhor(:,:)
real(dp),allocatable :: tauresid(:,:)
type(scf_history_type) :: scf_history_wf
type(paw_an_type),allocatable :: paw_an(:)
type(paw_ij_type),allocatable :: paw_ij(:)
type(pawfgrtab_type),allocatable,save :: pawfgrtab(:)
type(pawrhoij_type),pointer :: pawrhoij_ep(:)
type(fock_type),pointer :: fock
type(pawcprj_type),allocatable, target :: cprj_local(:,:)
! *********************************************************************
_IBM6("Hello, I'm running on IBM6")
DBG_ENTER("COLL")
call timab(238,1,tsec)
call timab(54,1,tsec)
call status(0,dtfil%filstat,iexit,level,'enter')
! enable time limit handler if not done in callers.
if (enable_timelimit_in(ABI_FUNC) == ABI_FUNC) then
write(std_out,*)"Enabling timelimit check in function: ",trim(ABI_FUNC)," with timelimit: ",trim(sec2str(get_timelimit()))
end if
! Initialise non_magnetic_xc for rhohxc
non_magnetic_xc=(dtset%usepawu==4).or.(dtset%usepawu==14)
!######################################################################
!Initializations - Memory allocations
!----------------------------------------------------------------------
lmax_diel = 0
!MPI communicators
if ((xmpi_paral==1).and.(mpi_enreg%paral_hf==1)) then
spaceComm=mpi_enreg%comm_kpt
else
spaceComm=mpi_enreg%comm_cell
end if
me=xmpi_comm_rank(spaceComm)
spaceComm_fft=mpi_enreg%comm_fft
spaceComm_wvl=mpi_enreg%comm_wvl
me_wvl=mpi_enreg%me_wvl
spaceComm_grid=mpi_enreg%comm_fft
if(dtset%usewvl==1) spaceComm_grid=mpi_enreg%comm_wvl
my_nspinor=max(1,dtset%nspinor/mpi_enreg%nproc_spinor)
!Save some variables from dataset definition
nstep=dtset%nstep
ecut=dtset%ecut
ecutf=ecut; if (psps%usepaw==1) ecutf=dtset%pawecutdg
if (psps%usepaw==1.and.pawfgr%usefinegrid==1) ecutf=dtset%pawecutdg
iscf10=mod(dtset%iscf,10)
tollist(1)=dtset%tolmxf;tollist(2)=dtset%tolwfr
tollist(3)=dtset%toldff;tollist(4)=dtset%toldfe
tollist(6)=dtset%tolvrs;tollist(7)=dtset%tolrff
tollist(8)=dtset%vdw_df_threshold
dielstrt=0
finite_efield_flag=(dtset%berryopt == 4 .or. &
& dtset%berryopt == 6 .or. &
& dtset%berryopt == 7 .or. &
& dtset%berryopt == 14 .or. &
& dtset%berryopt == 16 .or. &
& dtset%berryopt == 17)
!Get FFT grid(s) sizes (be careful !)
!See NOTES in the comments at the beginning of this file.
ngfft(:)=dtset%ngfft(:)
if (psps%usepaw==1) then
mgfftf=pawfgr%mgfft;ngfftf(:)=pawfgr%ngfft(:)
else
mgfftf=dtset%mgfft;ngfftf(:)=ngfft(:)
end if
!Calculate zion: the total positive charge acting on the valence electrons
zion=zero
do iatom=1,dtset%natom
zion=zion+psps%ziontypat(dtset%typat(iatom))
end do
!Compute different geometric tensor, as well as ucvol, from rprimd
call metric(gmet,gprimd,-1,rmet,rprimd,ucvol)
!Fock: be sure that the pointer is initialized to Null.
usefock=dtset%usefock
nullify(fock)
!Special care in case of WVL
!wvlbigdft indicates that the BigDFT workflow will be followed
wvlbigdft=(dtset%usewvl==1.and.dtset%wvl_bigdft_comp==1)
!if (wvlbigdft) then ! TO BE ACTIVATED LATER
! ABI_ALLOCATE(energies_wvl,)
!end if
ucvol_local = ucvol
#if defined HAVE_BIGDFT
if (dtset%usewvl == 1) then
! We need to tune the volume when wavelets are used because, not
! all FFT points are used.
! ucvol_local = (half * dtset%wvl_hgrid) ** 3 * ngfft(1)*ngfft(2)*ngfft(3)
ucvol_local = product(wvl%den%denspot%dpbox%hgrids) * real(product(wvl%den%denspot%dpbox%ndims), dp)
end if
#endif
!Some variables need to be initialized/nullified at start
nullify(grhor,lrhor,elfr)
quit=0 ; dbl_nnsclo=0 ; conv_retcode=0
dielop=0 ; strsxc=zero
deltae=zero ; elast=zero ;
vpotzero(:)=zero
! JWZ April 12 2018: Intel 18 compiler seems to require maxfor initialized,
! else it dies in scprqt in some scenarios
maxfor=zero
!
results_gs%residm=zero;results_gs%res2=zero
results_gs%deltae=zero;results_gs%diffor=zero
call energies_init(energies)
if (dtset%positron/=0.and.initialized/=0) then
energies%e0_electronpositron =results_gs%energies%e0_electronpositron
energies%e_electronpositron =results_gs%energies%e_electronpositron
energies%edc_electronpositron=results_gs%energies%edc_electronpositron
maxfor=zero
end if
! Initialize fermi level.
!if (dtset%nstep==0 .or. dtset%iscf < 0) then
if ((dtset%nstep==0 .or. dtset%iscf < 0) .and. dtset%plowan_compute==0) then
energies%e_fermie = results_gs%energies%e_fermie
results_gs%fermie = results_gs%energies%e_fermie
write(std_out,*)"in scfcv_core: results_gs%fermie: ",results_gs%fermie
end if
select case(dtset%usepotzero)
case(0,1)
energies%e_corepsp = ecore / ucvol
energies%e_corepspdc = zero
case(2)
! No need to include the PspCore energy since it is already included in the
! local pseudopotential (vpsp)
energies%e_corepsp = zero
energies%e_corepspdc = zero
end select
if(wvlbigdft) energies%e_corepsp = zero
fermie=energies%e_fermie
isave_den=0; isave_kden=0 !initial index of density protection file
optres=merge(0,1,dtset%iscf<10)
usexcnhat=0!;mcprj=0
initialized0=initialized
if (dtset%tfkinfunc==12) tfw_activated=.true.
ipert=0;idir=0;cplex=1
istep_mix=1
istep_fock_outer=1
ipositron=electronpositron_calctype(electronpositron)
unit_out=0;if (dtset%prtvol >= 10) unit_out=ab_out
nfftotf=product(ngfftf(1:3))
usecprj=0
if (mcprj>0) then
usecprj=1
end if
!Stresses and forces flags
forces_needed=0;prtfor=0
if ((dtset%optforces==1.or.dtset%ionmov==4.or.abs(tollist(3))>tiny(0._dp))) then
if (dtset%iscf>0.and.nstep>0) forces_needed=1
if (nstep==0) forces_needed=2
prtfor=1
else if (dtset%iscf>0.and.dtset%optforces==2) then
forces_needed=2
end if
stress_needed=0
if (dtset%optstress>0.and.dtset%iscf>0.and.dtset%prtstm==0.and. (nstep>0.or.dtfil%ireadwf==1)) stress_needed=1
if (dtset%optstress>0.and.dtset%iscf>0.and.psps%usepaw==1 &
& .and.finite_efield_flag.and.(nstep>0.or.dtfil%ireadwf==1)) stress_needed=1
!This is only needed for the tddft routine, and does not
!correspond to the intented use of results_gs (should be only
!for output of scfcv_core
etotal = results_gs%etotal
!Entering a scfcv_core loop, printing data to XML file if required.
prtxml=0;if (me==0.and.dtset%prtxml==1) prtxml=1
if (prtxml == 1) then
! scfcv_core() will handle a scf loop, so we output the scfcv markup.
write(ab_xml_out, "(A)") ' <scfcvLoop>'
write(ab_xml_out, "(A)") ' <initialConditions>'
! We output the geometry of the dataset given in argument.
! xred and rprimd are given independently since dtset only
! stores original and final values.
call out_geometry_XML(dtset, 4, dtset%natom, rprimd, xred)
write(ab_xml_out, "(A)") ' </initialConditions>'
end if
!Examine tolerance criteria, and eventually print a line to the output
!file (with choice=1, the only non-dummy arguments of scprqt are
!nstep, tollist and iscf - still, diffor and res2 are here initialized to 0)
choice=1 ; diffor=zero ; res2=zero
ABI_ALLOCATE(fcart,(3,dtset%natom))
ABI_ALLOCATE(fred,(3,dtset%natom))
fred(:,:)=zero
fcart(:,:)=results_gs%fcart(:,:) ! This is a side effect ...
!results_gs should not be used as input of scfcv_core
!HERE IS PRINTED THE FIRST LINE OF SCFCV
call scprqt(choice,cpus,deltae,diffor,dtset,&
& eigen,etotal,favg,fcart,energies%e_fermie,dtfil%fnameabo_app_eig,&
& dtfil%filnam_ds(1),initialized0,dtset%iscf,istep,dtset%kptns,&
& maxfor,moved_atm_inside,mpi_enreg,dtset%nband,dtset%nkpt,nstep,&
& occ,optres,prtfor,prtxml,quit,res2,resid,residm,response,tollist,&
& psps%usepaw,vxcavg,dtset%wtk,xred,conv_retcode)
!Various allocations (potentials, gradients, ...)
ABI_ALLOCATE(forold,(3,dtset%natom))
ABI_ALLOCATE(grchempottn,(3,dtset%natom))
ABI_ALLOCATE(gresid,(3,dtset%natom))
ABI_ALLOCATE(grewtn,(3,dtset%natom))
ABI_ALLOCATE(grnl,(3*dtset%natom))
ABI_ALLOCATE(grxc,(3,dtset%natom))
ABI_ALLOCATE(synlgr,(3,dtset%natom))
ABI_ALLOCATE(ph1d,(2,3*(2*dtset%mgfft+1)*dtset%natom))
ABI_ALLOCATE(ph1df,(2,3*(2*mgfftf+1)*dtset%natom))
ABI_ALLOCATE(vhartr,(nfftf))
ABI_ALLOCATE(vtrial,(nfftf,dtset%nspden))
ABI_ALLOCATE(vpsp,(nfftf))
ABI_ALLOCATE(vxc,(nfftf,dtset%nspden))
ABI_ALLOCATE(vxctau,(nfftf,dtset%nspden,4*dtset%usekden))
wfmixalg=dtset%fockoptmix/100
use_hybcomp=0
if(mod(dtset%fockoptmix,100)==11)use_hybcomp=1
ABI_ALLOCATE(vxc_hybcomp,(nfftf,dtset%nspden*use_hybcomp))
ngrvdw=0;if (dtset%vdw_xc>=5.and.dtset%vdw_xc<=7) ngrvdw=dtset%natom
ABI_ALLOCATE(grvdw,(3,ngrvdw))
grchempottn(:,:)=zero
forold(:,:)=zero ; gresid(:,:)=zero ; pel(:)=zero
vtrial(:,:)=zero; vxc(:,:)=zero
n1xccc=0;if (psps%n1xccc/=0) n1xccc=psps%n1xccc
n3xccc=0;if (psps%n1xccc/=0) n3xccc=nfftf
ABI_ALLOCATE(xccc3d,(n3xccc))
!Allocations/initializations for PAW only
lpawumax=-1
if(psps%usepaw==1) then
! Variables/arrays related to the fine FFT grid
ABI_ALLOCATE(nhat,(nfftf,dtset%nspden*psps%usepaw))
if (nstep==0) nhat=zero
ABI_DATATYPE_ALLOCATE(pawfgrtab,(my_natom))
if (my_natom>0) then
call pawtab_get_lsize(pawtab,l_size_atm,my_natom,dtset%typat,&
& mpi_atmtab=mpi_enreg%my_atmtab)
call pawfgrtab_init(pawfgrtab,cplex,l_size_atm,dtset%nspden,dtset%typat,&
& mpi_atmtab=mpi_enreg%my_atmtab,comm_atom=mpi_enreg%comm_atom)
ABI_DEALLOCATE(l_size_atm)
end if
compch_fft=-1.d5
usexcnhat=maxval(pawtab(:)%usexcnhat)
if (usexcnhat==0.and.dtset%ionmov==4.and.dtset%iscf<10) then
message = 'You cannot simultaneously use ionmov=4 and such a PAW psp file !'
MSG_ERROR(message)
end if
! Variables/arrays related to the PAW spheres
ABI_DATATYPE_ALLOCATE(paw_ij,(my_natom))
ABI_DATATYPE_ALLOCATE(paw_an,(my_natom))
call paw_an_nullify(paw_an)
call paw_ij_nullify(paw_ij)
has_dijhat=0;if (dtset%iscf==22) has_dijhat=1
has_vhartree=0; if (dtset%prtvha > 0 .or. dtset%prtvclmb > 0) has_vhartree=1
has_dijfock=0; if (usefock==1) has_dijfock=1
has_dijnd=0;if(any(abs(dtset%nucdipmom)>tol8)) has_dijnd=1
has_dijU=0; if (dtset%usepawu==5.or.dtset%usepawu==6) has_dijU=1
call paw_an_init(paw_an,dtset%natom,dtset%ntypat,0,0,dtset%nspden,&
& cplex,dtset%pawxcdev,dtset%typat,pawang,pawtab,has_vxc=1,has_vxc_ex=1,has_vhartree=has_vhartree,&
& comm_atom=mpi_enreg%comm_atom,mpi_atmtab=mpi_enreg%my_atmtab)
call paw_ij_init(paw_ij,cplex,dtset%nspinor,dtset%nsppol,dtset%nspden,&
& dtset%pawspnorb,dtset%natom,dtset%ntypat,dtset%typat,pawtab,&
& has_dij=1,has_dijfock=has_dijfock,has_dijhartree=1,has_dijnd=has_dijnd,has_dijso=1,has_dijhat=has_dijhat,&
& has_dijU=has_dijU,has_pawu_occ=1,has_exexch_pot=1,nucdipmom=dtset%nucdipmom,&
& comm_atom=mpi_enreg%comm_atom,mpi_atmtab=mpi_enreg%my_atmtab)
if(dtset%usewvl==1) then
call paw2wvl_ij(1,paw_ij,wvl%descr)
end if
compch_sph=-1.d5
ABI_ALLOCATE(dimcprj,(dtset%natom))
ABI_ALLOCATE(dimcprj_srt,(dtset%natom))
call pawcprj_getdim(dimcprj ,dtset%natom,nattyp,dtset%ntypat,dtset%typat,pawtab,'R')
call pawcprj_getdim(dimcprj_srt,dtset%natom,nattyp,dtset%ntypat,dtset%typat,pawtab,'O')
do itypat=1,dtset%ntypat
if (pawtab(itypat)%usepawu>0) lpawumax=max(pawtab(itypat)%lpawu,lpawumax)
end do
if (dtset%usedmatpu/=0.and.lpawumax>0) then
if (2*lpawumax+1/=size(dmatpawu,1).or.2*lpawumax+1/=size(dmatpawu,2)) then
message = 'Incorrect size for dmatpawu!'
MSG_BUG(message)
end if
end if
! Allocation of projected WF (optional)
if (usecprj==1) then
iorder_cprj=0
if (usefock==1) then
ctocprj_choice = 1
if (dtset%optforces == 1) then
ctocprj_choice = 2; ! ncpgr = 3
end if
! if (dtset%optstress /= 0) then
! ncpgr = 6 ; ctocprj_choice = 3
! end if
end if
#if defined HAVE_BIGDFT
if (dtset%usewvl==1) then
mband_cprj=dtset%mband;if (dtset%paral_kgb/=0) mband_cprj=mband_cprj/mpi_enreg%nproc_band
mcprj_wvl=my_nspinor*mband_cprj*dtset%mkmem*dtset%nsppol
ABI_DATATYPE_ALLOCATE(wvl%descr%paw%cprj,(dtset%natom,mcprj_wvl))
call cprj_paw_alloc(wvl%descr%paw%cprj,0,dimcprj_srt)
end if
#endif
end if
! Other variables for PAW
nullify(pawrhoij_ep);if(associated(electronpositron))pawrhoij_ep=>electronpositron%pawrhoij_ep
nullify(lmselect_ep);if(associated(electronpositron))lmselect_ep=>electronpositron%lmselect_ep
else
ABI_ALLOCATE(dimcprj,(0))
ABI_ALLOCATE(dimcprj_srt,(0))
ABI_ALLOCATE(nhat,(0,0))
ABI_DATATYPE_ALLOCATE(paw_ij,(0))
ABI_DATATYPE_ALLOCATE(paw_an,(0))
ABI_DATATYPE_ALLOCATE(pawfgrtab,(0))
end if ! PAW
!Several parameters and arrays for the SCF mixing:
!These arrays are needed only in the self-consistent case
if (dtset%iscf>=0) then
dielar(1)=dtset%diecut;dielar(2)=dtset%dielng
dielar(3)=dtset%diemac;dielar(4)=dtset%diemix
dielar(5)=dtset%diegap;dielar(6)=dtset%dielam
dielar(7)=dtset%diemix;if (dtset%iscf>=10) dielar(7)=dtset%diemixmag
ABI_ALLOCATE(nvresid,(nfftf,dtset%nspden))
ABI_ALLOCATE(tauresid,(nfftf,dtset%nspden*dtset%usekden))
if (nstep==0) then
nvresid=zero
tauresid=zero
end if
ABI_ALLOCATE(dtn_pc,(3,dtset%natom))
! The next arrays are needed if iscf==5 and ionmov==4,
! but for the time being, they are always allocated
ABI_ALLOCATE(grhf,(3,dtset%natom))
! Additional allocation for mixing within PAW
npawmix=0
if(psps%usepaw==1) then
do iatom=1,my_natom
itypat=pawrhoij(iatom)%itypat
pawrhoij(iatom)%use_rhoijres=1
sz1=pawrhoij(iatom)%cplex*pawtab(itypat)%lmn2_size
sz2=pawrhoij(iatom)%nspden
ABI_ALLOCATE(pawrhoij(iatom)%rhoijres,(sz1,sz2))
do ispden=1,pawrhoij(iatom)%nspden
pawrhoij(iatom)%rhoijres(:,ispden)=zero
end do
ABI_ALLOCATE(pawrhoij(iatom)%kpawmix,(pawtab(itypat)%lmnmix_sz))
pawrhoij(iatom)%lmnmix_sz=pawtab(itypat)%lmnmix_sz
pawrhoij(iatom)%kpawmix=pawtab(itypat)%kmix
npawmix=npawmix+pawrhoij(iatom)%nspden*pawtab(itypat)%lmnmix_sz*pawrhoij(iatom)%cplex
end do
end if
if (dtset%iscf > 0) then
denpot = AB7_MIXING_POTENTIAL
if (dtset%iscf > 10) denpot = AB7_MIXING_DENSITY
if (psps%usepaw==1.and.dtset%pawmixdg==0 .and. dtset%usewvl==0) then
ispmix=AB7_MIXING_FOURRIER_SPACE;nfftmix=dtset%nfft;ngfftmix(:)=ngfft(:)
else
ispmix=AB7_MIXING_REAL_SPACE;nfftmix=nfftf;ngfftmix(:)=ngfftf(:)
end if
!TRangel: added to avoid segfaults with Wavelets
nfftmix_per_nfft=0;if(nfftf>0) nfftmix_per_nfft=(1-nfftmix/nfftf)
call ab7_mixing_new(mix, iscf10, denpot, ispmix, nfftmix, dtset%nspden, npawmix, errid, message, dtset%npulayit)
if (errid /= AB7_NO_ERROR) then
MSG_ERROR(message)
end if
if (dtset%mffmem == 0) then
call ab7_mixing_use_disk_cache(mix, dtfil%fnametmp_fft)
end if
! else if (dtset%iscf==0.and.dtset%usewvl==1) then
! ispmix=AB7_MIXING_REAL_SPACE;nfftmix=nfftf;ngfftmix(:)=ngfftf(:)
end if
else
ABI_ALLOCATE(nvresid,(0,0))
ABI_ALLOCATE(tauresid,(0,0))
ABI_ALLOCATE(dtn_pc,(0,0))
ABI_ALLOCATE(grhf,(0,0))
end if ! iscf>0
!Here, allocate arrays for computation of susceptibility and dielectric matrix or for TDDFT
if( (nstep>0 .and. dtset%iscf>=0) .or. dtset%iscf==-1 ) then !MF
! Here, for TDDFT, artificially set iprcel . Also set a variable to reduce the memory needs.
afford=1
if(dtset%iscf==-1) then
! dtset%iprcel=21
afford=0
end if
! First compute dimensions
if(dtset%iprcel>=21 .or. dtset%iscf==-1)then
! With dielop=1, the matrices will be computed when istep=dielstrt
! With dielop=2, the matrices will be computed when istep=dielstrt and 1
dielop=1
if(dtset%iprcel>=41)dielop=2
if((dtset%iprcel >= 71).and.(dtset%iprcel<=79)) dielop=0 !RSkerker preconditioner do not need the susceptibility matrix
! Immediate computation of dielectric matrix
dielstrt=1
! Or delayed computation
if(modulo(dtset%iprcel,100)>21 .and. modulo(dtset%iprcel,100)<=29)dielstrt=modulo(dtset%iprcel,100)-20
if(modulo(dtset%iprcel,100)>31 .and. modulo(dtset%iprcel,100)<=39)dielstrt=modulo(dtset%iprcel,100)-30
if(modulo(dtset%iprcel,100)>41 .and. modulo(dtset%iprcel,100)<=49)dielstrt=modulo(dtset%iprcel,100)-40
if(modulo(dtset%iprcel,100)>51 .and. modulo(dtset%iprcel,100)<=59)dielstrt=modulo(dtset%iprcel,100)-50
if(modulo(dtset%iprcel,100)>61 .and. modulo(dtset%iprcel,100)<=69)dielstrt=modulo(dtset%iprcel,100)-60
! Get diecut, and the fft grid to be used for the susceptibility computation
diecut=abs(dtset%diecut)
if( dtset%diecut<0.0_dp )then
ecutsus=ecut
else
ecutsus= ( sqrt(ecut) *0.5_dp + sqrt(diecut) *0.25_dp )**2
end if
! Impose sequential calculation
ngfftdiel(1:3)=0 ; ngfftdiel(7)=100 ; ngfftdiel(9)=0; ngfftdiel(8)=dtset%ngfft(8);ngfftdiel(10:18)=0
if(dtset%iscf==-1)ngfftdiel(7)=102
! The dielectric stuff is performed in sequential mode; set mpi_enreg_diel accordingly
call initmpi_seq(mpi_enreg_diel)
call getng(dtset%boxcutmin,ecutsus,gmet,k0,mpi_enreg_diel%me_fft,mgfftdiel,nfftdiel,ngfftdiel,&
& mpi_enreg_diel%nproc_fft,dtset%nsym,mpi_enreg_diel%paral_kgb,dtset%symrel,&
& use_gpu_cuda=dtset%use_gpu_cuda)
! Update the fft distribution
call init_distribfft_seq(mpi_enreg_diel%distribfft,'c',ngfftdiel(2),ngfftdiel(3),'all')
! Compute the size of the dielectric matrix
kpt_diel(1:3)=(/ 0.0_dp, 0.0_dp, 0.0_dp /)
call getmpw(diecut,dtset%exchn2n3d,gmet,(/1/),kpt_diel,mpi_enreg_diel,npwdiel,1)
lmax_diel=0
if (psps%usepaw==1) then
do ii=1,dtset%ntypat
lmax_diel=max(lmax_diel,pawtab(ii)%lcut_size)
end do
end if
else
npwdiel=1
mgfftdiel=1
nfftdiel=1
lmax_diel=0
afford=0
end if
! Now, performs allocation
ABI_ALLOCATE(dielinv,(2,npwdiel*afford,dtset%nspden,npwdiel,dtset%nspden))
ABI_ALLOCATE(susmat,(2,npwdiel*afford,dtset%nspden,npwdiel,dtset%nspden))
ABI_ALLOCATE(kg_diel,(3,npwdiel))
ABI_ALLOCATE(gbound_diel,(2*mgfftdiel+8,2))
ABI_ALLOCATE(irrzondiel,(nfftdiel**(1-1/dtset%nsym),2,(dtset%nspden/dtset%nsppol)-3*(dtset%nspden/4)))
ABI_ALLOCATE(phnonsdiel,(2,nfftdiel**(1-1/dtset%nsym),(dtset%nspden/dtset%nsppol)-3*(dtset%nspden/4)))
ABI_ALLOCATE(ph1ddiel,(2,3*(2*mgfftdiel+1)*dtset%natom*psps%usepaw))
ABI_ALLOCATE(ylmdiel,(npwdiel,lmax_diel**2))
! Then, compute the values of different arrays
if(dielop>=1)then
! Note : npwarr_diel is dummy, npwtot_diel is dummy
! This kpgio call for going from the suscep FFT grid to the diel sphere
npwarr_diel(1)=npwdiel
call kpgio(diecut,dtset%exchn2n3d,gmet,(/1/),kg_diel,&
& kpt_diel,1,(/1/),1,'COLL',mpi_enreg_diel,npwdiel,&
& npwarr_diel,npwtot_diel,dtset%nsppol)
call sphereboundary(gbound_diel,1,kg_diel,mgfftdiel,npwdiel)
if (dtset%nsym>1 .and. dtset%iscf>=0 ) then
! Should replace this initialization of irrzondiel and phnonsdiel through setsym by a direct call to irrzg
ABI_ALLOCATE(indsym_dum,(4,dtset%nsym,dtset%natom))
ABI_ALLOCATE(symrec_dum,(3,3,dtset%nsym))
call setsym(indsym_dum,irrzondiel,dtset%iscf,dtset%natom,&
& nfftdiel,ngfftdiel,dtset%nspden,dtset%nsppol,dtset%nsym,phnonsdiel,&
& dtset%symafm,symrec_dum,dtset%symrel,dtset%tnons,dtset%typat,xred)
ABI_DEALLOCATE(indsym_dum)
ABI_DEALLOCATE(symrec_dum)
end if
if (psps%usepaw==1) then
call getph(atindx,dtset%natom,ngfftdiel(1),ngfftdiel(2),&
& ngfftdiel(3),ph1ddiel,xred)
call initylmg(gprimd,kg_diel,kpt_diel,1,mpi_enreg_diel,&
& lmax_diel,npwdiel,dtset%nband,1,npwarr_diel,dtset%nsppol,0,&
& rprimd,ylmdiel,rhodum)
end if
end if
if(dtset%iprcel>=21 .or. dtset%iscf==-1)then
call destroy_mpi_enreg(mpi_enreg_diel)
end if
else
npwdiel=1
mgfftdiel=1
nfftdiel=1
afford = 0
ABI_ALLOCATE(susmat,(0,0,0,0,0))
ABI_ALLOCATE(kg_diel,(0,0))
ABI_ALLOCATE(gbound_diel,(0,0))
ABI_ALLOCATE(irrzondiel,(0,0,0))
ABI_ALLOCATE(phnonsdiel,(0,0,0))
ABI_ALLOCATE(ph1ddiel,(0,0))
ABI_ALLOCATE(ylmdiel,(0,0))
end if
nkxc=0
!TDDFT - For a first coding
if (dtset%iscf==-1 .and. dtset%nspden==1) nkxc=2
if (dtset%iscf==-1 .and. dtset%nspden==2) nkxc=3
!Eventually need kxc-LDA when susceptibility matrix has to be computed
if (dtset%iscf>0.and.modulo(dtset%iprcel,100)>=61.and.(dtset%iprcel<71.or.dtset%iprcel>79)) nkxc=2*min(dtset%nspden,2)-1
!Eventually need kxc-LDA for residual forces (when density mixing is selected)
if (dtset%iscf>=10.and.dtset%usewvl==0.and.forces_needed>0 .and. &
& abs(dtset%densfor_pred)>=1.and.abs(dtset%densfor_pred)<=6.and.abs(dtset%densfor_pred)/=5) then
if (dtset%xclevel==1.or.dtset%densfor_pred>=0) nkxc=2*min(dtset%nspden,2)-1
if (dtset%xclevel==2.and.dtset%nspden==1.and.dtset%densfor_pred<0) nkxc=7
if (dtset%xclevel==2.and.dtset%nspden==2.and.dtset%densfor_pred<0) nkxc=19
end if
if (nkxc>0) then
call check_kxc(dtset%ixc,dtset%optdriver)
end if
ABI_ALLOCATE(kxc,(nfftf,nkxc))
!This flag will be set to 1 just before an eventual change of atomic
!positions inside the iteration, and set to zero when the consequences
!of this change are taken into account.
moved_atm_inside=0
!This flag will be set to 1 if the forces are computed inside the iteration.
computed_forces=0
if(dtset%wfoptalg==2)then
ABI_ALLOCATE(shiftvector,((dtset%mband+2)*dtset%nkpt))
val_min=-1.0_dp
val_max=zero
else
ABI_ALLOCATE(shiftvector,(1))
end if
call status(0,dtfil%filstat,iexit,level,'berryphase ')
!!PAW+DMFT: allocate structured datatype paw_dmft if dtset%usedmft=1
!call init_sc_dmft(dtset%dmftbandi,dtset%dmftbandf,dtset%mband,dtset%nkpt,&
!& dtset%nsppol,dtset%usedmft,paw_dmft,dtset%usedmft)
!call print_sc_dmft(paw_dmft)
!!Electric field initializations: initialize pel_cg(:) and p_ion(:)
call update_e_field_vars(atindx,atindx1,cg,dimcprj,dtefield,dtfil,dtset,&
& efield_old_cart,gmet,gprimd,hdr,idir,kg,mcg,&
& dtset%mkmem,mpi_enreg,dtset%mpw,my_natom,dtset%natom,nattyp,ngfft,dtset%nkpt,npwarr,&
& dtset%ntypat,pawrhoij,pawtab,pel_cg,pelev,pion,psps,ptot,ptot_cart,&
& pwind,pwind_alloc,pwnsfac,red_efield2,red_efield2_old,red_ptot,rmet,rprimd,&
& 0,quit,istep,ucvol,unit_out,psps%usepaw,xred,ylm,ylmgr)
if (dtset%iscf==22) energies%h0=zero
call timab(54,2,tsec)
!##################################################################
!PERFORM ELECTRONIC ITERATIONS
!##################################################################
!Offer option of computing total energy with existing
!wavefunctions when nstep<=0, else do nstep iterations
!Note that for non-self-consistent calculations, this loop will be exited
!after the first call to vtorho
!Pass through the first routines even when nstep==0
quitsum_request = xmpi_request_null; timelimit_exit = 0
istep_updatedfock=0
! start SCF loop
do istep=1,max(1,nstep)