forked from alexei-matveev/paragauss-gpl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpks_g4constructs.f90
2410 lines (2033 loc) · 80.9 KB
/
cpks_g4constructs.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
!
! ParaGauss, a program package for high-performance computations of
! molecular systems
!
! Copyright (C) 2014 T. Belling, T. Grauschopf, S. Krüger,
! F. Nörtemann, M. Staufer, M. Mayer, V. A. Nasluzov, U. Birkenheuer,
! A. Hu, A. V. Matveev, A. V. Shor, M. S. K. Fuchs-Rohr, K. M. Neyman,
! D. I. Ganyushin, T. Kerdcharoen, A. Woiterski, A. B. Gordienko,
! S. Majumder, M. H. i Rotllant, R. Ramakrishnan, G. Dixit,
! A. Nikodem, T. Soini, M. Roderus, N. Rösch
!
! This program is free software; you can redistribute it and/or modify
! it under the terms of the GNU General Public License version 2 as
! published by the Free Software Foundation [1].
!
! 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.
!
! [1] http://www.gnu.org/licenses/gpl-2.0.html
!
! Please see the accompanying LICENSE file for further information.
!
!=====================================================================
! Public interface of module
!=====================================================================
subroutine cpks_g4constructs()
! output : cpks%b
# define b_on_file
! define FPP_TIMERS 2
# include "def.h"
use type_module ! standard data types
use interfaces, only: calc_cpks_gvec, calc_cpks_h1imp
use symmetry_data_module
use datatype ! user defined data types
use comm_module
use comm, only: comm_allreduce
use msgtag_module
use iounitadmin_module ! provides io units
use print_module !interface for outroutines
use filename_module ! set I/O-Filenames
use occupation_module, only: get_n_elec
use eigen_data_module, ONLY : eigval,eigvec
use fit_coeff_module
use bounds_module, only: bounds_ch, bounds_calc
use pairs_module
use pert_coeff_module
use linsys_module
use init_module, only: init
use mat_charge_module
use occupied_levels_module, ONLY: eigval_occ
use calc3c_switches
use cpksdervs_matrices
use xc_cntrl, only: xc_is_on=>is_on, xc_ANY
use post_scf_module
use gradient_data_module, only: cpks_gradient_totalsym, gradient_index
use comm, only: comm_bcast
use grid_module,only:grid_close
use operations_module, only: operations_solvation_effect !!!!!!!!!!!!AS
use solv_2nd_deriv_module, only: cpks_solv_main,cpks_solv_Qai,cpks_solv_H1,cpks_solv_H1_1 !!!!!!!!!!!!!!!AS
use readwriteblocked_module
USE_MEMLOG
implicit none
type(fit) :: n_fit
integer(kind=i4_kind) :: n_ai, n_occ, n_vir, j_cpks, n_holes
! ilower/iupper : charge fit functions
integer(kind=i4_kind) :: info, iupper, ilower,&
i_i,i_g,i_spn, &
n_spin, n_irrep, n_ch, n_xc, n_mat, n_proj
integer(kind=i4_kind) :: i_grad
real(kind=r8_kind) :: target_charge
real(kind=r8_kind),dimension(:,:),allocatable :: d1_temp
real(kind=r8_kind),allocatable :: QxCOai(:),GxQxCOai(:,:),cpks_gamma(:,:),cpks_betta(:,:,:)
! the core fit functions are distributed precisely as the charge fit functions
external error_handler
real(kind=r8_kind), allocatable:: help_cpks_gvec(:,:)
integer(kind=i4_kind):: k
real(kind=r8_kind):: gamma_eps=10.0e-10
integer(kind=i4_kind)::msgtag_start_cpksgvec=186
integer(kind=i4_kind)::msgtag_done_cpksgvec=188
type(readwriteblocked_tapehandle):: cpks_b_th(0:n_cpks+1)
type(readwriteblocked_tapehandle):: th_mo_coul,th_s1,th_h1ai,th_h1
integer(kind=i4_kind)::n_split
character(len=2) :: b_fn
logical:: cpks_ser_limited
! ----------------------------------------------------------------
! context: all processors
FPP_TIMER_START(t_cpks_const)
FPP_TIMER_START(t_cpks_init)
call print_cpksalloc()
MEMSET(0)
n_split=n_cpks_split
call get_cpks_dims() ! get sizes before h1ai deallocate
if(.not.comm_i_am_master()) then
call dealocate_h1ai_s1ai()
else
call tofile_h1es1_ai() ! deallocates h1ai and s1ai
endif
call say(" cpks h1ai and s1ai deallocated ")
! s1 and h1 here is stil in memory
call tofile_h1(th_h1)
call print_cpksalloc()
comm_dervs_xc=.false.
n_spin = symmetry_data_n_spin()
n_irrep = symmetry_data_n_irreps()
n_proj = 1
cpks(:,:,:)%n_fin_cyc=-1 ! n_fin_cyc nedds to be defined
! this is a mask, if ge.0 then these
! data are not treated futher on
cpks_gradient_index=>gradient_index
call get_fit(n_fit)
n_ch = n_fit%n_ch
n_xc = n_fit%n_xc
n_mat = (n_ch*(n_ch+1))/2
if(comm_i_am_master()) then
! FIXME: cpks_gamma(0:n_cpks+1,...) and at the end of sub
! there is s access to 1+i_cpks element of it
allocate(cpks_gamma(0:n_cpks+1,size(cpks,1)), &
cpks_betta(0:n_cpks,0:n_cpks,size(cpks,1)), &
stat=cpksalloc(118))
ASSERT(cpksalloc(118).eq.0)
MEMLOG(size(cpks_gamma)+size(cpks_betta))
cpks_gamma=0.0_r8_kind
cpks_betta=0.0_r8_kind
endif
if(comm_i_am_master()) call hole_corrected_s1() ! s1 is first required here
! load eigvec_vir and eigval_vir on each slave
ASSERT(allocated(eigval))
ASSERT(allocated(eigvec))
do i_g = 1, n_irrep
call comm_bcast(eigvec(i_g)%m)
do i_grad = 1, size(cpks,1)
do i_spn = 1, n_spin
call comm_bcast(cpks(i_grad, i_g, i_spn)%s1)
enddo
enddo
enddo
call tofile_s1(th_s1)
call print_cpksalloc
if(.not.allocated(cpks_gmat)) then
allocate(cpks_gmat(n_ch,n_ch),stat=cpksalloc(4))
MEMLOG(size(cpks_gmat))
ASSERT(cpksalloc(4).eq.0)
endif
if(comm_i_am_master()) call calc_cpks_gmat()
call comm_bcast(cpks_gmat)
call fromfile_s1(th_s1)
#if 1 /* calculate cpks_gvec */
master_distribute_work: if(comm_i_am_master()) then
call say(" calculate cpks_gvec ")
! FIXME: shouldnt it have been already done?
call bounds_calc(n_fit)
! First start the slaves
to_slaves: if (comm_parallel() ) then
!
! Skip the share of master:
!
ilower = bounds_ch(1) + 1
slaves :do i_i = 2, comm_get_n_processors()
iupper = ilower + bounds_ch(i_i) - 1
call comm_init_send(i_i,msgtag_start_cpksgvec) !msgtag_cpks_gvec) start calc cpks_gvec
call commpack(ilower,info)
ASSERT(info.eq.0)
call commpack(iupper,info)
ASSERT(info.eq.0)
call comm_send()
!
! Move forward:
!
ilower = ilower + bounds_ch(i_i)
enddo slaves
endif to_slaves
! Then start the master
! pert_vec actually holds the charge density projections [rho_spin|f_k]
ilower = 1
iupper = bounds_ch(1)
call calc_cpks_gvec(ilower, iupper) ! now on master, depends on cpks%s1
#ifndef memprof
if(comm_parallel()) then ! receive cpks_gvec contribs from slave
#endif
allocate(help_cpks_gvec(size(cpks_gvec,1),size(cpks_gvec,2)),stat=cpksalloc(124))
ASSERT(cpksalloc(124).eq.0)
MEMLOG(size(help_cpks_gvec))
#ifndef memprof
from_slaves :do i_i=2,comm_get_n_processors()
call comm_save_recv(i_i,msgtag_packed_message)
call communpack(help_cpks_gvec(1,1),size(cpks_gvec),1,info)
ASSERT(info.eq.0)
cpks_gvec=cpks_gvec+help_cpks_gvec
enddo from_slaves
#endif
MEMLOG(-size(help_cpks_gvec))
deallocate(help_cpks_gvec,stat=cpksalloc(124))
ASSERT(cpksalloc(124).eq.0)
cpksalloc(124)=1
#ifndef memprof
call comm_init_send(comm_all_other_hosts,msgtag_done_cpksgvec) !all slave contribs in
call comm_send
endif
#endif
else master_distribute_work !i.e. now on slave
call comm_save_recv(comm_master_host,msgtag_start_cpksgvec)
call communpack(ilower,info)
ASSERT(info.eq.0)
call communpack(iupper,info)
ASSERT(info.eq.0)
call calc_cpks_gvec(ilower, iupper) ! s1 imp now on slave
!
! result: cpks_gvec to be used in Q calcs
!
! occupied orbital coefficiens direvatives contibs calculated with
! use of 3 center inegrals and overlap derivative s1 matrix
! array of (n_ch,n_grad) shape where all occ MO contribs summed up
!
! result to be communicated to all hosts to be used for Qai calcs
call comm_init_send(comm_master_host,msgtag_packed_message)
call commpack(cpks_gvec(1,1),size(cpks_gvec),1,info)
call comm_send()
call comm_save_recv(comm_master_host,msgtag_done_cpksgvec) !master done with cpks_gvec
endif master_distribute_work !/else
#endif /* calculate cpks_gvec */
FPP_TIMER_STOP(t_cpks_init)
DPRINT 't_cpks_init calc_cpks_gvec=', FPP_TIMER_VALUE(t_cpks_init)
FPP_TIMER_START(t_cpks_init)
call say(" calculate co_ai ")
#define mo_coul_onfile
#define no_co_ai
#ifndef no_co_ai
call allocate_co_ai()
#endif
call calc_cpks3c_ai(ilower,iupper, th_mo_coul)
#define split_co_ai
#ifdef mo_coul_onfile
#ifndef no_co_ai
call write_mo_coul()
call deallocate_co_ai() !!!!!!!
#endif
call say(" cpks mo_coul stored on file ")
#endif
FPP_TIMER_STOP(t_cpks_init)
DPRINT 't_cpks_init 3c_ai calculated', FPP_TIMER_VALUE(t_cpks_init)
call allocate_Q_B() ! in reg run B is on file
#if 0
call allocate_AB()
DPRINT 'allocate_Q_AB_B_HBH done'
print*,'max_cpks_coul_grad_size co_ai_size cpks_size cpks_fitmat_size ', &
max_cpks_coul_grad_size, co_ai_size,cpks_size,cpks_fitmat_size
#endif
FPP_TIMER_START(t_cpks_init)
!!$ DPRINT ' '
!!$ DPRINT 'sum s1(i_grad) and sum abs s1(i_grad) to calc xc part of GxSkl'
!!$ do i_spn=1,size(cpks,3)
!!$ do i_grad=1,size(cpks,1)
!!$!!! cpks(i_grad,1,i_spn)%h1=0.0_r8_kind
!!$ DPRINT sum(cpks(i_grad,1,i_spn)%s1),sum(abs(cpks(i_grad,1,i_spn)%s1)),cpks(i_grad,1,i_spn)%s1(1,1)
!!$ ! if(size(cpks(i_grad,1,i_spn)%s1).gt.1) &
!!$ ! print*,cpks(i_grad,1,i_spn)%s1(2,2),cpks(i_grad,1,i_spn)%s1(1,2),cpks(i_grad,1,i_spn)%s1(2,1)
!!$ enddo
!!$ enddo
!!$
!!$
!!$ DPRINT ' '
call fromfile_h1(th_h1)
![[== XC call 1 ===================================================
if( xc_is_on(xc_ANY) )then
FPP_TIMER_START(t_dervs_xc)
call say(" cpks Qxc factors ")
call print_cpksalloc()
cpks_Qxc_calculated=.false.
call cpks_xc_main(21) ! output: Qai xc contrib, func h1 contrib
cpks_Qxc_calculated=.true.
FPP_TIMER_STOP(t_dervs_xc)
DPRINT 't_dervs_xc Qxc',FPP_TIMER_VALUE(t_dervs_xc)
#if 0
call print_Qai()
#endif
!!$ DPRINT ' h1 xc contrib',hai
!!$ do i_grad=1,size(cpks,1)
!!$ if(n_spin.eq.2) then
!!$ DPRINT sum(cpks(i_grad,1,1)%h1),sum(cpks(i_grad,1,2)%h1) ,i_grad
!!$ DPRINT cpks(i_grad,1,1)%h1(1,1),cpks(i_grad,1,2)%h1(1,1)
!!$ else
!!$ DPRINT sum(cpks(i_grad,1,1)%h1)
!!$ endif
!!$ enddo
endif
!]]== eof XC call 1 ===============================================
FPP_TIMER_STOP(t_cpks_init)
FPP_TIMER_START(t_cpks_init)
#if 1
call allocate_AB()
DPRINT 'allocate_Q_AB_B_HBH done'
DPRINT 'max_cpks_coul_grad_size=', max_cpks_coul_grad_size
DPRINT 'co_ai_size=', co_ai_size
DPRINT 'cpks_size=', cpks_size
DPRINT 'cpks_fitmat_size=', cpks_fitmat_size
#endif
allocate (cpks_fitcoeff_grads(n_fit%n_ch,size(cpks,1)),stat=cpksalloc(108))
! MEMLOG(size(cpks_fitcoeff_grads)) ! deallocated in main_integral
ASSERT(cpksalloc(108).eq.0)
master_cpks_grad_totalsym: if(comm_i_am_master()) then
! explicit dependence fit coeff derivatives from 3 center and 2 center
! coulombs integral variation
!!$ DPRINT '[cpks_gradient_totalsym control sum] and [derivatives of a] - (cpks_gradient_totalsym*cpks_Gmat) '
!!$ do i_grad=1,size(cpks,1)
!!$ DPRINT sum(cpks_gradient_totalsym(:,i_grad)*coeff_charge)
!!$ DPRINT sum(matmul(cpks_Gmat,cpks_gradient_totalsym(:,i_grad)))
!!$ enddo
!--------------------------------------------------------------
allocate(d1_temp(size(cpks_gradient_totalsym,1), &
size(cpks_gradient_totalsym,2)),stat=cpksalloc(24))
MEMLOG(size(d1_temp))
ASSERT(cpksalloc(24).eq.0)
d1_temp=cpks_gradient_totalsym ! both 2 and 3 center contribs
call dgemm('n','n', n_ch,size(cpks_gradient_totalsym,2),n_ch, &
1.0_r8_kind,cpks_Gmat,n_ch,d1_temp,n_ch, &
0.0_r8_kind,cpks_gradient_totalsym,n_ch)
!!$ DPRINT ' d1(cpks_gradient_totalsym) contribs to a_k derivatives'
!!$ do i_grad=1,size(cpks,1)
!!$ DPRINT sum(cpks_gradient_totalsym(:,i_grad)),i_grad,sum(d1_temp(:,i_grad))
!!$ enddo
MEMLOG(-size(d1_temp))
deallocate(d1_temp,stat=cpksalloc(24))
ASSERT(cpksalloc(24).eq.0)
cpksalloc(24)=1
endif master_cpks_grad_totalsym
cpks_fitcoeff_grads(:,:)=cpks_gradient_totalsym-cpks_gvec/n_spin
! cpks_gvec is not split by spins as a contrib to cpks_fitcoeff_grads
! which should be the same for both spins ????
call comm_bcast(cpks_fitcoeff_grads)
! broadcasted to be used in calc_cpksQai
call calc_cpksQai(th_mo_coul,ilower,iupper) ! coul contrib
if(operations_solvation_effect) call cpks_solv_Qai() !!!!!!!!!!!!!!!AS depends on s1
call tofile_s1(th_s1)
! s1 is not to be used to the end of subroutine and thus can be
! presently stored on tape and temporary deallocated
! next use to calculate complite first derivatives of hamiltonian
call reduce_Qai()
if(comm_i_am_master()) then
DPRINT 'call fromfile_h1ai'
#if 0
call fromfile_h1es1_ai()
#endif
call assembl_Qai() ! Qai+h1ai+s1ai, h1ai and s1ai are not used any more
endif
#if 0
DPRINT 'Qai assembled'
call print_Qai()
if(size(cpks(1,1,1)%h1ai).gt.0) then
DPRINT 'h1ai(1,1)', cpks(1,1,1)%h1ai(1,1),cpks(1,1,1)%h1ai(size(cpks(1,1,1)%h1ai,1),1)
endif
#endif
#if 0
if(comm_i_am_master()) call dealocate_h1ai_s1ai()
#endif
MEMLOG(-size(cpks_gvec))
deallocate (cpks_gvec,stat=cpksalloc(3)) ! allocated in calc_cpks_gvec
ASSERT(cpksalloc(3).eq.0)
cpksalloc(3)=1
FPP_TIMER_STOP(t_cpks_init)
DPRINT 't_cpks_init Q coul calculated', FPP_TIMER_VALUE(t_cpks_init)
allocate(QxCOai(ilower:iupper),GxQxCOai(n_ch,size(cpks,1)),stat=cpksalloc(34))
ASSERT(cpksalloc(34).eq.0)
MEMLOG(size(QxCOai)+size(GxQxCOai))
#if 0 /* debug tool */
call tofix_mo()
#endif
if(comm_i_am_master()) call b0() ! cpks_gamma(0)
#ifdef b_on_file
call deallocate_Q()
#endif
call allocate_HBH()
#ifndef b_on_file
call hole_corrected_b(0)
#else
call hole_corrected_b()
#endif
FPP_TIMER_START(t_cpks_init)
call coul_imp_grads(GxQxCOai,th_mo_coul,cpks_gmat) ! (1)
call comm_allreduce(GxQxCOai)
#if 0
call allocate_AB()
DPRINT 'allocate_Q_AB_B_HBH done'
print*,'max_cpks_coul_grad_size co_ai_size cpks_size cpks_fitmat_size ', &
max_cpks_coul_grad_size, co_ai_size,cpks_size,cpks_fitmat_size
#endif
call calc_ab(th_mo_coul) !(1)
FPP_TIMER_STOP(t_cpks_init)
i_cpks=0
j_cpks=0
![[== XC call 2 ===================================================
if( xc_is_on(xc_ANY) )then
FPP_TIMER_START(t_dervs_xc)
call cpks_xc_main(22) ! contrib to A matrix, i_cpks=0
FPP_TIMER_STOP(t_dervs_xc)
DPRINT 't_dervs_xc i_cpks=0 contrib to A matrix',FPP_TIMER_VALUE(t_dervs_xc)
endif
!]]== eof XC call 2 ===============================================
if(operations_solvation_effect) call cpks_solv_main() !!!!!!!!!!!!!!!AS
call reduce_ABai() !co_ai
if(comm_i_am_master()) then
#ifdef b_on_file
! print warning
do i_grad=1,size(cpks,1)
if(abs(cpks_gamma(0,i_grad)).ge.gamma_eps) cycle
DPRINT 'cpks calculations for projection are skeeped'
enddo
call eigval_weight_ab()
call readwriteblocked_startread(trim(tmpfile('cpks_b_0')), cpks_b_th(0))
! calculate betta(0,0)
betta_spin_loop: do i_spn=1,n_spin
betta_irr: do i_g=1,n_irrep
n_ai=cpks3c(i_g,i_spn)%n_ai
if(n_ai <= 0) cycle
betta_grads: do i_grad=1,size(cpks,1)
do k=1,size(cpks(i_grad,i_g,i_spn)%HBH,2)
call readwriteblocked_read(cpks(i_grad,i_g,i_spn)%HBH(:,k),cpks_b_th(0))
enddo
if(abs(cpks_gamma(0,i_grad)).lt.gamma_eps) then
cpks(i_grad,i_g,i_spn)%n_fin_cyc=0
else ! exist for cpks_gamma(0) .ne. 0
cpks_betta(0,0,i_grad)=cpks_betta(0,0,i_grad)+&
real(symmetry_data_n_partners(i_g),r8_kind)/cpks_gamma(0,i_grad)* &
sum(cpks(i_grad,i_g,i_spn)%ABi*cpks(i_grad,i_g,i_spn)%HBH)
endif
enddo betta_grads
enddo betta_irr
enddo betta_spin_loop
call readwriteblocked_stopread(cpks_b_th(0),'keep')
! calculate cpks_gamma(1)
call readwriteblocked_startwrite(trim(tmpfile('cpks_b_1')), cpks_b_th(1))
bplus1_spin_loop: do i_spn=1,n_spin
bplus1_irr: do i_g=1,n_irrep
n_ai=cpks3c(i_g,i_spn)%n_ai
if(n_ai.le.0) cycle
bplus1_grads: do i_grad=1,size(cpks,1)
cpks(i_grad,i_g,i_spn)%m=0.0_r8_kind
cpks(i_grad,i_g,i_spn)%m(0,0)=1.0_r8_kind-cpks_betta(0,0,i_grad)
cpks(i_grad,i_g,i_spn)%ABi=cpks(i_grad,i_g,i_spn)%ABi &
-cpks_betta(0,0,i_grad)*cpks(i_grad,i_g,i_spn)%HBH
cpks_gamma(1,i_grad)=cpks_gamma(1,i_grad)+symmetry_data_n_partners(i_g)*&
sum(cpks(i_grad,i_g,i_spn)%ABi**2)
do k=1,size(cpks(i_grad,i_g,i_spn)%HBH,2)
call readwriteblocked_write(cpks(i_grad,i_g,i_spn)%ABi(:,k),cpks_b_th(1)) !{2)
enddo
enddo bplus1_grads
enddo bplus1_irr
enddo bplus1_spin_loop
call readwriteblocked_stopwrite(cpks_b_th(1))
#else
betta_spin_loop: do i_spn=1,n_spin
betta_irr: do i_g=1,n_irrep
n_ai=cpks3c(i_g,i_spn)%n_ai
if(n_ai <= 0) cycle
betta_grads: do i_grad=1,size(cpks,1)
if(abs(cpks_gamma(0,i_grad)).lt.gamma_eps) then
cpks(i_grad,i_g,i_spn)%n_fin_cyc=0
else ! exist for cpks_gamma(0) .ne. 0
cpks_betta(0,0,i_grad)=cpks_betta(0,0,i_grad)+&
real(symmetry_data_n_partners(i_g),r8_kind)/cpks_gamma(0,i_grad)* &
sum(cpks(i_grad,i_g,i_spn)%ABi(:,:)*cpks(i_grad,i_g,i_spn)%B(:,:,0))
endif
enddo betta_grads
enddo betta_irr
enddo betta_spin_loop
bplus1_spin_loop: do i_spn=1,n_spin
bplus1_irr: do i_g=1,n_irrep
n_ai=cpks3c(i_g,i_spn)%n_ai
if(n_ai.le.0) cycle
bplus1_grads: do i_grad=1,size(cpks,1)
cpks(i_grad,i_g,i_spn)%m=0.0_r8_kind
cpks(i_grad,i_g,i_spn)%m(0,0)=1.0_r8_kind-cpks_betta(0,0,i_grad)
cpks(i_grad,i_g,i_spn)%B(:,:,1)=cpks(i_grad,i_g,i_spn)%ABi(:,:) &
-cpks_betta(0,0,i_grad)*cpks(i_grad,i_g,i_spn)%B(:,:,0)
cpks_gamma(1,i_grad)=cpks_gamma(1,i_grad)+symmetry_data_n_partners(i_g)*&
sum(cpks(i_grad,i_g,i_spn)%B(:,:,1)**2)
enddo bplus1_grads
enddo bplus1_irr
enddo bplus1_spin_loop
#endif
endif ! i_am_master
fill_cpks_i: do i_cpks=1,n_cpks
DPRINT i_cpks,'i_cpks_________________________________________________________'
DPRINT 'HBH sum'
! ** main result B-matrix for (i_cpks+1)
FPP_TIMER_START(t_cpks_init)
#ifdef b_on_file
call hole_corrected_b()
#else
call hole_corrected_b(i_cpks)
#endif
call coul_imp_grads(GxQxCOai,th_mo_coul,cpks_gmat) !(2)
call comm_allreduce(GxQxCOai)
call calc_ab(th_mo_coul) !(2)
FPP_TIMER_STOP(t_cpks_init)
![[== XC call 3 ===================================================
if( xc_is_on(xc_ANY) )then
FPP_TIMER_START(t_dervs_xc)
call cpks_xc_main(23) ! contrib to A, i_cpks
FPP_TIMER_STOP(t_dervs_xc)
DPRINT 't_dervs_xc i_cpks contrib to A matrix',FPP_TIMER_VALUE(t_dervs_xc),i_cpks
endif
!]]== eof XC call 3 ===============================================
if(operations_solvation_effect) call cpks_solv_main() !!!!!!!!!!!!!!!AS
call reduce_ABai()
fill_cpks_master: if(comm_i_am_master()) then
fill_gamma_spin: do i_spn=1,n_spin
fill_gamma_irr: do i_g=1,n_irrep
n_ai=cpks3c(i_g,i_spn)%n_ai
if(n_ai.le.0) cycle
fill_gamma_grads: do i_grad=1,size(cpks,1)
do n_occ=1,size(cpks(1,i_g,i_spn)%ABi,1)
do n_vir=1,size(cpks(1,i_g,i_spn)%ABi,2)
if(abs(eigval_occ(i_g)%m(n_occ,i_spn) &
-cpks3c(i_g,i_spn)%eigval_vir_and_holes(n_vir)).gt.0.000000001_r8_kind) then
cpks(i_grad,i_g,i_spn)%ABi(n_occ,n_vir)=cpks(i_grad,i_g,i_spn)%ABi(n_occ,n_vir) &
/(eigval_occ(i_g)%m(n_occ,i_spn)-cpks3c(i_g,i_spn)%eigval_vir_and_holes(n_vir))
else
cpks(i_grad,i_g,i_spn)%ABi(n_occ,n_vir)=0.0_r8_kind
endif
enddo
enddo
enddo fill_gamma_grads
enddo fill_gamma_irr
enddo fill_gamma_spin
! fill cpks_betta
#ifndef b_on_file
fill_betta_spin: do i_spn=1,n_spin
fill_betta_irr: do i_g=1,n_irrep
n_ai=cpks3c(i_g,i_spn)%n_ai
if(cpks3c(i_g,i_spn)%n_ai.le.0) cycle
fill_betta_grads: do i_grad=1,size(cpks,1)
cpks(i_grad,i_g,i_spn)%B(:,:,i_cpks+1)=cpks(i_grad,i_g,i_spn)%ABi(:,:)
! ABi will not be used below exept this loop thus it can be hole occupation
! modified to calc betta factors
if(cpks(i_grad,i_g,i_spn)%n_fin_cyc.ge.0) cycle
betta_cpks_j: do j_cpks=0,i_cpks
call dgemm('t','n', 1,1,n_ai, &
real(symmetry_data_n_partners(i_g),r8_kind)/cpks_gamma(j_cpks,i_grad), &
cpks(i_grad,i_g,i_spn)%ABi(:,:),n_ai, &
cpks(i_grad,i_g,i_spn)%B(:,:,j_cpks),n_ai, &
1.0_r8_kind,cpks_betta(j_cpks,i_cpks,i_grad),1)
enddo betta_cpks_j
enddo fill_betta_grads
enddo fill_betta_irr
enddo fill_betta_spin
#else
betta_cpks_j: do j_cpks=0,i_cpks
write(b_fn,'(i2)') j_cpks
call readwriteblocked_startread(trim(tmpfile('cpks_b_'//adjustl(b_fn))), cpks_b_th(j_cpks))
fill_betta_spin: do i_spn=1,n_spin
fill_betta_irr: do i_g=1,n_irrep
n_ai=cpks3c(i_g,i_spn)%n_ai
if(cpks3c(i_g,i_spn)%n_ai.le.0) cycle
fill_betta_grads: do i_grad=1,size(cpks,1)
do k=1,size(cpks(i_grad,i_g,i_spn)%HBH,2)
call readwriteblocked_read(cpks(i_grad,i_g,i_spn)%HBH(:,k),cpks_b_th(j_cpks))
enddo
if(cpks(i_grad,i_g,i_spn)%n_fin_cyc.ge.0) cycle !no beta calc if converged by i_cpks
if(abs(cpks_gamma(i_cpks,i_grad)).le.gamma_eps) cycle
call dgemm('t','n', 1,1,n_ai, &
real(symmetry_data_n_partners(i_g),r8_kind)/cpks_gamma(j_cpks,i_grad), &
cpks(i_grad,i_g,i_spn)%ABi(:,:),n_ai, &
cpks(i_grad,i_g,i_spn)%HBH(:,:),n_ai, &
1.0_r8_kind,cpks_betta(j_cpks,i_cpks,i_grad),1)
enddo fill_betta_grads
enddo fill_betta_irr
enddo fill_betta_spin
call readwriteblocked_stopread(cpks_b_th(j_cpks),'keep')
enddo betta_cpks_j
#endif /* b_on_file */
do i_grad=1,size(cpks,1)
do i_g=1,n_irrep
do i_spn=1,n_spin
if(cpks(i_grad,i_g,i_spn)%n_fin_cyc.ge.0) cycle
cpks(i_grad,i_g,i_spn)%m(i_cpks,i_cpks)=1.0_r8_kind-cpks_betta(i_cpks,i_cpks,i_grad)
cpks(i_grad,i_g,i_spn)%m(i_cpks,i_cpks-1)=-1.0_r8_kind
do j_cpks=0,i_cpks-1
cpks(i_grad,i_g,i_spn)%m(j_cpks,i_cpks)=-cpks_betta(j_cpks,i_cpks,i_grad)
enddo
enddo
enddo
enddo
call b_plus(i_cpks)
! set dependent on gamma(i_cpks) mask
cpks_ser_limited=.false.
do i_grad=1,size(cpks,1)
if(abs(cpks_gamma(i_cpks,i_grad)).ge.gamma_eps ) cycle
do i_spn=1,n_spin
do i_g=1,n_irrep
if(cpks(i_grad,i_g,i_spn)%n_fin_cyc.ge.0) cycle
cpks(i_grad,i_g,i_spn)%n_fin_cyc=i_cpks
cpks_ser_limited=.true.
enddo
enddo
enddo
if(cpks_ser_limited) then
DPRINT 'cpks_ser_limited i_cpks', i_cpks
endif
endif fill_cpks_master
enddo fill_cpks_i
!-------------------------------------------------------------------------------------------
if(comm_i_am_master()) then
call solve_cpks() !!! result: B(NCPKS+1)
endif
FPP_TIMER_START(t_cpks_init)
#ifndef b_on_file
call hole_corrected_b(n_cpks+1)
#else
call hole_corrected_b()
#endif
call coul_imp_grads(GxQxCOai,th_mo_coul,cpks_gmat) !(3)
call comm_allreduce(GxQxCOai)
if(comm_i_am_master()) then
MEMLOG(-size(cpks_gamma)-size(cpks_betta))
deallocate(cpks_gamma, cpks_betta, stat=cpksalloc(118))
ASSERT(cpksalloc(118).eq.0)
cpksalloc(118)=1
endif
if(comm_i_am_master()) then
cpks_fitcoeff_grads=cpks_fitcoeff_grads+GxQxCOai
endif
call comm_bcast(cpks_fitcoeff_grads)
! ! brodcated to be used in calc_cpks_h1imp
call calc_ab(th_mo_coul,'delete') !(3) here co_ai is last time used
#ifndef mo_coul_onfile
call deallocate_co_ai()
#endif
FPP_TIMER_STOP(t_cpks_init)
i_cpks=n_cpks+1 ! now check obtained result
! and calculate density matrix contrib
! to fitcoeff derivatives
!!$ DPRINT '%h1 func before xc imp calcs------',sum(abs(cpks(1,1,1)%h1(:,:)))
![[== XC call 4 ===================================================
if( xc_is_on(xc_ANY) )then
call say("implicite xc dervs ")
FPP_TIMER_START(t_dervs_xc)
comm_dervs_xc=.true. ! set on to communicate dervs_xc
call cpks_xc_main(24) ! contrib to A matrix, i_cpks=n_cpks+1 (last of 3 calls)
! result: dependent on B h1 contrib
comm_dervs_xc=.false. ! set off for next postscf call
FPP_TIMER_STOP(t_dervs_xc)
DPRINT 't_dervs_xc n_cpks+1 contrib to A matrix',FPP_TIMER_VALUE(t_dervs_xc),i_cpks
endif
!]]== eof XC call 4 ===============================================
if(operations_solvation_effect) call cpks_solv_main() !!!!!!!!!!!!!!!AS
call reduce_ABai()
call cpks_convergence()
#ifndef b_on_file
if(comm_i_am_master()) call deallocate_B()
call deallocate_Q()
#endif
call deallocate_AB()
DPRINT 't_cpks_init coul_imp_grads', FPP_TIMER_VALUE(t_cpks_init)
FPP_TIMER_START(t_cpks_init)
! s1 is used here for h1 check thus is to be swapt to memory
! next use is wor calculation of w1
call fromfile_s1(th_s1)
if(operations_solvation_effect) call cpks_solv_H1() !!!!!!!!!!!!!!!AS
if(operations_solvation_effect) call cpks_solv_H1_1() !!!!!!!!!!!!!!!AS
call calc_cpks_h1imp(ilower, iupper) !!! s1 undependent
FPP_TIMER_STOP(t_cpks_init)
DPRINT 't_cpks_init calc_cpks_h1imp', FPP_TIMER_VALUE(t_cpks_init)
if(comm_parallel()) call collect_h1()
#if 0 /* convenient check on h1 */
h1_irr: do i_g=1,n_irrep
n_occ=size(cpks(1,i_g,1)%h1,1)
if(n_occ.gt.0) then
print*
print*,'%h1 func+imp',cpks(1,i_g,1)%h1(1,1),cpks(1,i_g,1)%h1(n_occ,n_occ)
do n_occ=1,size(cpks(1,i_g,1)%h1,1)
print*,cpks(1,i_g,1)%h1(n_occ,:)
print*,'%h1-%s1*eig_p check',n_occ,cpks(1,i_g,1)%h1(n_occ,n_occ) &
-eigval(i_g)%m(n_occ,1)*cpks(1,i_g,1)%s1(n_occ,n_occ), &
eigval(i_g)%m(n_occ,1)
enddo
endif !n_occ.gt.0
enddo h1_irr
#endif
call hole_corrected_h1()
! if(operations_solvation_effect) then
! call cpks_solv_ham()
! print*,'solv h0',sum(cpks(1,1,1)%Qai),sum(cpks(2,1,1)%Qai)
! call calc_solv_ham_mo('ai')
! endif
if(allocated(cpks_gmat)) then
MEMLOG(-size(cpks_gmat))
deallocate(cpks_gmat,stat=cpksalloc(4))
ASSERT(cpksalloc(4).eq.0)
cpksalloc(4)=1
endif
MEMLOG(-size(QxCOai)-size(GxQxCOai))
deallocate(QxCOai,GxQxCOai,stat=cpksalloc(34))
ASSERT(cpksalloc(34).eq.0)
cpksalloc(34)=1
FPP_TIMER_STOP(t_cpks_const)
#ifdef FPP_TIMERS
print*,'t_s1_imp_grarho =',FPP_TIMER_VALUE(t_s1_imp_grarho)
print*,'t_s1_imp_dervsrho=',FPP_TIMER_VALUE(t_s1_imp_dervsrho)
print*,'t_xc_qh_expl =',FPP_TIMER_VALUE(t_xc_qh_expl)
print*,'t_h1q_dvdrho =',FPP_TIMER_VALUE(t_h1q_dvdrho)
print*,'t_dervs_helps =',FPP_TIMER_VALUE(t_dervs_helps)
print*,'t_cpks_init =',FPP_TIMER_VALUE(t_cpks_init)
print*,'t_xc_bimp_grarho gragamma =',FPP_TIMER_VALUE(t_xc_bimp_grarho)
print*,'t_xc_ab =',FPP_TIMER_VALUE(t_xc_ab)
print*,'t_dervs_imp =',FPP_TIMER_VALUE(t_dervs_imp)
print*,'t_calc_imp_dervs=', FPP_TIMER_VALUE(t_calc_imp_dervs)
print*,'t_grid_unique tot =',FPP_TIMER_VALUE(t_grid_uni)
print*,'t_orbital_calculate = ',FPP_TIMER_VALUE(t_orbital_calc)
print*,'t_density_calculate=', FPP_TIMER_VALUE(t_density_calc)
print*,'t_dervs_xc tot =',FPP_TIMER_VALUE(t_dervs_xc)
print*,'t_cpks_const =',FPP_TIMER_VALUE(t_cpks_const)
print*, 'cpks_constructs mem controls'
#endif
MEMSET(0)
call grid_close(.false.)
contains
subroutine calc_cpks_gmat()
real(kind=r8_kind),allocatable:: cpks_inv_chmat(:,:)
real(kind=r8_kind),dimension(: ),allocatable :: inv_linsys_mat ! for cpks equations
real(kind=r8_kind),dimension(: ),allocatable :: linsys_mat,linsys_dual
call get_n_elec(target_charge)
! Linear solver
! note that size of linsys_dual is n_ch + 1 !
allocate(linsys_mat((n_ch*(n_ch+1))/2),linsys_dual(n_ch+1),STAT=cpksalloc(5))
MEMLOG(size(linsys_mat)+size(linsys_dual))
ASSERT(cpksalloc(5).eq.0)
linsys_mat = mat_charge
call decompose_linsys(n_ch,linsys_mat,charge_norm,linsys_dual)
allocate(inv_linsys_mat(n_ch*(n_ch+1)/2),stat=cpksalloc(116))
ASSERT(cpksalloc(116).eq.0)
MEMLOG(size(inv_linsys_mat))
allocate(cpks_inv_chmat(n_ch,n_ch),stat=cpksalloc(19))
ASSERT(cpksalloc(19).eq.0)
MEMLOG(size(cpks_inv_chmat))
inv_linsys_mat=linsys_mat
call gmat_linsys(n_ch,inv_linsys_mat,charge_norm, &
linsys_dual, cpks_gmat,cpks_inv_chmat)
MEMLOG(-size(inv_linsys_mat)-size(cpks_inv_chmat))
deallocate(inv_linsys_mat,cpks_inv_chmat,stat=cpksalloc(19))
ASSERT(cpksalloc(19).eq.0)
cpksalloc(19)=1
cpksalloc(116)=1
DPRINT ' cpks_g4constructs: cpks_gmat is calculated' ,sum(cpks_gmat)
MEMLOG(-size(linsys_dual)-size(linsys_mat))
deallocate(linsys_dual,linsys_mat,STAT=cpksalloc(5))
ASSERT(cpksalloc(5).eq.0)
cpksalloc(5)=1
end subroutine calc_cpks_gmat
#ifdef mo_coul_onfile
#ifndef no_co_ai
subroutine write_mo_coul()
integer(kind=i4_kind):: i_spn,i_g,k,i_occ
call readwriteblocked_startwrite(trim(tmpfile('mo_coul.dat')), th_mo_coul)
do i_spn=1,size(cpks3c,2)
do i_g=1,size(cpks3c,1)
do k=1,size(cpks3c(i_g,i_spn)%co_ai,1)
do i_occ=1,size(cpks3c(i_g,i_spn)%co_ai,2)
call readwriteblocked_write(cpks3c(i_g,i_spn)%co_ai(k,i_occ,:),th_mo_coul)
enddo
enddo
enddo
enddo
call readwriteblocked_stopwrite(th_mo_coul)
end subroutine write_mo_coul
#endif
#endif
subroutine tofile_h1es1_ai()
integer(kind=i4_kind):: i_spn,i_g,i_occ,i_grad,stat,j_vir
call readwriteblocked_startwrite(trim(tmpfile('h1ai.dat')), th_h1ai)
do i_spn=1,size(cpks,3)
do i_g=1,size(cpks,2)
do i_grad=1,size(cpks,1)
if(size(cpks(i_grad,i_g,i_spn)%h1ai).ne.0) then
do i_occ=1,size(cpks(i_grad,i_g,i_spn)%h1ai,1)
do j_vir=1,size(cpks(i_grad,i_g,i_spn)%h1ai,2)
cpks(i_grad,i_g,i_spn)%h1ai(i_occ,j_vir) = cpks(i_grad,i_g,i_spn)%h1ai(i_occ,j_vir) &
-cpks(i_grad,i_g,i_spn)%s1ai(i_occ,j_vir)*eigval(i_g)%m(i_occ,i_spn)
enddo
call readwriteblocked_write(cpks(i_grad,i_g,i_spn)%h1ai(i_occ,:),th_h1ai)
enddo
endif
deallocate(cpks(i_grad,i_g,i_spn)%h1ai,cpks(i_grad,i_g,i_spn)%s1ai,stat=stat)
ASSERT(stat.eq.0)
cpksalloc(167)=1 ! h1ai
cpksalloc(162)=1
enddo
enddo
enddo
call readwriteblocked_stopwrite(th_h1ai)
end subroutine tofile_h1es1_ai
subroutine fromfile_h1es1_ai()