-
Notifications
You must be signed in to change notification settings - Fork 1
/
qs_loc_utils.F
1717 lines (1517 loc) · 80.1 KB
/
qs_loc_utils.F
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
!--------------------------------------------------------------------------------------------------!
! CP2K: A general program to perform molecular dynamics simulations !
! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
!> \brief Some utilities for the construction of
!> the localization environment
!> \author MI (05-2005)
! **************************************************************************************************
MODULE qs_loc_utils
USE ai_moments, ONLY: contract_cossin,&
cossin
USE basis_set_types, ONLY: gto_basis_set_p_type,&
gto_basis_set_type
USE block_p_types, ONLY: block_p_type
USE cell_types, ONLY: cell_type,&
pbc
USE cp_array_utils, ONLY: cp_1d_r_p_type
USE cp_control_types, ONLY: dft_control_type
USE cp_dbcsr_api, ONLY: dbcsr_copy,&
dbcsr_get_block_p,&
dbcsr_p_type,&
dbcsr_set,&
dbcsr_type
USE cp_dbcsr_operations, ONLY: cp_dbcsr_sm_fm_multiply
USE cp_files, ONLY: close_file,&
open_file
USE cp_fm_basic_linalg, ONLY: cp_fm_column_scale
USE cp_fm_diag, ONLY: choose_eigv_solver
USE cp_fm_struct, ONLY: cp_fm_struct_create,&
cp_fm_struct_release,&
cp_fm_struct_type
USE cp_fm_types, ONLY: &
cp_fm_create, cp_fm_get_info, cp_fm_get_submatrix, cp_fm_release, cp_fm_set_all, &
cp_fm_set_submatrix, cp_fm_to_fm, cp_fm_type, cp_fm_write_unformatted
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_get_default_io_unit,&
cp_logger_type,&
cp_to_string
USE cp_output_handling, ONLY: cp_p_file,&
cp_print_key_finished_output,&
cp_print_key_generate_filename,&
cp_print_key_should_output,&
cp_print_key_unit_nr
USE distribution_1d_types, ONLY: distribution_1d_type
USE input_constants, ONLY: &
do_loc_crazy, do_loc_direct, do_loc_gapo, do_loc_jacobi, do_loc_l1_norm_sd, do_loc_none, &
do_loc_scdm, energy_loc_range, op_loc_berry, op_loc_boys, op_loc_pipek, state_loc_all, &
state_loc_list, state_loc_mixed, state_loc_none, state_loc_range
USE input_section_types, ONLY: section_vals_get_subs_vals,&
section_vals_type,&
section_vals_val_get
USE kinds, ONLY: default_path_length,&
default_string_length,&
dp
USE mathconstants, ONLY: twopi
USE memory_utilities, ONLY: reallocate
USE message_passing, ONLY: mp_para_env_type
USE orbital_pointers, ONLY: ncoset
USE parallel_gemm_api, ONLY: parallel_gemm
USE particle_types, ONLY: particle_type
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type
USE qs_kind_types, ONLY: get_qs_kind,&
get_qs_kind_set,&
qs_kind_type
USE qs_loc_types, ONLY: get_qs_loc_env,&
localized_wfn_control_create,&
localized_wfn_control_release,&
localized_wfn_control_type,&
qs_loc_env_type,&
set_qs_loc_env
USE qs_localization_methods, ONLY: initialize_weights
USE qs_mo_methods, ONLY: make_mo_eig
USE qs_mo_types, ONLY: get_mo_set,&
mo_set_type
USE qs_neighbor_list_types, ONLY: get_iterator_info,&
neighbor_list_iterate,&
neighbor_list_iterator_create,&
neighbor_list_iterator_p_type,&
neighbor_list_iterator_release,&
neighbor_list_set_p_type
USE qs_scf_types, ONLY: ot_method_nr
USE scf_control_types, ONLY: scf_control_type
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
! *** Global parameters ***
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'qs_loc_utils'
! *** Public ***
PUBLIC :: qs_loc_env_init, loc_write_restart, &
retain_history, qs_loc_init, compute_berry_operator, &
set_loc_centers, set_loc_wfn_lists, qs_loc_control_init
CONTAINS
! **************************************************************************************************
!> \brief copy old mos to new ones, allocating as necessary
!> \param mo_loc_history ...
!> \param mo_loc ...
! **************************************************************************************************
SUBROUTINE retain_history(mo_loc_history, mo_loc)
TYPE(cp_fm_type), DIMENSION(:), POINTER :: mo_loc_history
TYPE(cp_fm_type), DIMENSION(:), INTENT(IN) :: mo_loc
CHARACTER(len=*), PARAMETER :: routineN = 'retain_history'
INTEGER :: handle, i, ncol_hist, ncol_loc
CALL timeset(routineN, handle)
IF (.NOT. ASSOCIATED(mo_loc_history)) THEN
ALLOCATE (mo_loc_history(SIZE(mo_loc)))
DO i = 1, SIZE(mo_loc_history)
CALL cp_fm_create(mo_loc_history(i), mo_loc(i)%matrix_struct)
END DO
END IF
DO i = 1, SIZE(mo_loc_history)
CALL cp_fm_get_info(mo_loc_history(i), ncol_global=ncol_hist)
CALL cp_fm_get_info(mo_loc(i), ncol_global=ncol_loc)
CPASSERT(ncol_hist == ncol_loc)
CALL cp_fm_to_fm(mo_loc(i), mo_loc_history(i))
END DO
CALL timestop(handle)
END SUBROUTINE retain_history
! **************************************************************************************************
!> \brief rotate the mo_new, so that the orbitals are as similar
!> as possible to ones in mo_ref.
!> \param mo_new ...
!> \param mo_ref ...
!> \param matrix_S ...
! **************************************************************************************************
SUBROUTINE rotate_state_to_ref(mo_new, mo_ref, matrix_S)
TYPE(cp_fm_type), INTENT(IN) :: mo_new, mo_ref
TYPE(dbcsr_type), POINTER :: matrix_S
CHARACTER(len=*), PARAMETER :: routineN = 'rotate_state_to_ref'
INTEGER :: handle, ncol, ncol_ref, nrow
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: eigenvalues
TYPE(cp_fm_struct_type), POINTER :: fm_struct_tmp
TYPE(cp_fm_type) :: o1, o2, o3, o4, smo
CALL timeset(routineN, handle)
CALL cp_fm_get_info(mo_new, nrow_global=nrow, ncol_global=ncol)
CALL cp_fm_get_info(mo_ref, ncol_global=ncol_ref)
CPASSERT(ncol == ncol_ref)
NULLIFY (fm_struct_tmp)
CALL cp_fm_create(smo, mo_ref%matrix_struct)
CALL cp_fm_struct_create(fm_struct_tmp, nrow_global=ncol, &
ncol_global=ncol, para_env=mo_new%matrix_struct%para_env, &
context=mo_new%matrix_struct%context)
CALL cp_fm_create(o1, fm_struct_tmp)
CALL cp_fm_create(o2, fm_struct_tmp)
CALL cp_fm_create(o3, fm_struct_tmp)
CALL cp_fm_create(o4, fm_struct_tmp)
CALL cp_fm_struct_release(fm_struct_tmp)
! o1 = (mo_new)^T matrix_S mo_ref
CALL cp_dbcsr_sm_fm_multiply(matrix_S, mo_ref, smo, ncol)
CALL parallel_gemm('T', 'N', ncol, ncol, nrow, 1.0_dp, mo_new, smo, 0.0_dp, o1)
! o2 = (o1^T o1)
CALL parallel_gemm('T', 'N', ncol, ncol, ncol, 1.0_dp, o1, o1, 0.0_dp, o2)
! o2 = (o1^T o1)^-1/2
ALLOCATE (eigenvalues(ncol))
CALL choose_eigv_solver(o2, o3, eigenvalues)
CALL cp_fm_to_fm(o3, o4)
eigenvalues(:) = 1.0_dp/SQRT(eigenvalues(:))
CALL cp_fm_column_scale(o4, eigenvalues)
CALL parallel_gemm('N', 'T', ncol, ncol, ncol, 1.0_dp, o3, o4, 0.0_dp, o2)
! o3 = o1 (o1^T o1)^-1/2
CALL parallel_gemm('N', 'N', ncol, ncol, ncol, 1.0_dp, o1, o2, 0.0_dp, o3)
! mo_new o1 (o1^T o1)^-1/2
CALL parallel_gemm('N', 'N', nrow, ncol, ncol, 1.0_dp, mo_new, o3, 0.0_dp, smo)
CALL cp_fm_to_fm(smo, mo_new)
! XXXXXXX testing
! CALL parallel_gemm('N','T',ncol,ncol,ncol,1.0_dp,o3,o3,0.0_dp,o1)
! WRITE(*,*) o1%local_data
! CALL parallel_gemm('T','N',ncol,ncol,ncol,1.0_dp,o3,o3,0.0_dp,o1)
! WRITE(*,*) o1%local_data
CALL cp_fm_release(o1)
CALL cp_fm_release(o2)
CALL cp_fm_release(o3)
CALL cp_fm_release(o4)
CALL cp_fm_release(smo)
CALL timestop(handle)
END SUBROUTINE rotate_state_to_ref
! **************************************************************************************************
!> \brief allocates the data, and initializes the operators
!> \param qs_loc_env new environment for the localization calculations
!> \param localized_wfn_control variables and directives for the localization
!> \param qs_env the qs_env in which the qs_env lives
!> \param myspin ...
!> \param do_localize ...
!> \param loc_coeff ...
!> \param mo_loc_history ...
!> \par History
!> 04.2005 created [MI]
!> \author MI
!> \note
!> similar to the old one, but not quite
! **************************************************************************************************
SUBROUTINE qs_loc_env_init(qs_loc_env, localized_wfn_control, qs_env, myspin, do_localize, &
loc_coeff, mo_loc_history)
TYPE(qs_loc_env_type), POINTER :: qs_loc_env
TYPE(localized_wfn_control_type), POINTER :: localized_wfn_control
TYPE(qs_environment_type), POINTER :: qs_env
INTEGER, INTENT(IN), OPTIONAL :: myspin
LOGICAL, INTENT(IN), OPTIONAL :: do_localize
TYPE(cp_fm_type), DIMENSION(:), INTENT(IN), &
OPTIONAL :: loc_coeff
TYPE(cp_fm_type), DIMENSION(:), OPTIONAL, POINTER :: mo_loc_history
CHARACTER(len=*), PARAMETER :: routineN = 'qs_loc_env_init'
INTEGER :: dim_op, handle, i, iatom, imo, imoloc, &
ispin, j, l_spin, lb, nao, naosub, &
natoms, nmo, nmosub, nspins, s_spin, ub
REAL(KIND=dp) :: my_occ, occ_imo
REAL(KIND=dp), DIMENSION(:), POINTER :: occupations
REAL(KIND=dp), DIMENSION(:, :), POINTER :: vecbuffer
TYPE(cell_type), POINTER :: cell
TYPE(cp_fm_struct_type), POINTER :: tmp_fm_struct
TYPE(cp_fm_type), DIMENSION(:), POINTER :: moloc_coeff
TYPE(cp_fm_type), POINTER :: mat_ptr, mo_coeff
TYPE(dbcsr_p_type), DIMENSION(:), POINTER :: matrix_s
TYPE(distribution_1d_type), POINTER :: local_molecules
TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
CALL timeset(routineN, handle)
NULLIFY (mos, matrix_s, moloc_coeff, particle_set, para_env, cell, local_molecules, occupations, mat_ptr)
IF (PRESENT(do_localize)) qs_loc_env%do_localize = do_localize
IF (qs_loc_env%do_localize) THEN
CALL get_qs_env(qs_env=qs_env, matrix_s=matrix_s, cell=cell, &
local_molecules=local_molecules, particle_set=particle_set, &
para_env=para_env, mos=mos)
nspins = SIZE(mos, 1)
s_spin = 1
l_spin = nspins
IF (PRESENT(myspin)) THEN
s_spin = myspin
l_spin = myspin
END IF
ALLOCATE (moloc_coeff(s_spin:l_spin))
DO ispin = s_spin, l_spin
NULLIFY (tmp_fm_struct, mo_coeff)
CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff, nao=nao, nmo=nmo)
nmosub = localized_wfn_control%nloc_states(ispin)
CALL cp_fm_struct_create(tmp_fm_struct, nrow_global=nao, &
ncol_global=nmosub, para_env=para_env, context=mo_coeff%matrix_struct%context)
CALL cp_fm_create(moloc_coeff(ispin), tmp_fm_struct)
CALL cp_fm_get_info(moloc_coeff(ispin), nrow_global=naosub, &
ncol_global=nmosub)
CPASSERT(nao == naosub)
IF ((localized_wfn_control%do_homo) .OR. &
(localized_wfn_control%set_of_states == state_loc_mixed)) THEN
CPASSERT(nmo >= nmosub)
ELSE
CPASSERT(nao - nmo >= nmosub)
END IF
CALL cp_fm_set_all(moloc_coeff(ispin), 0.0_dp)
CALL cp_fm_struct_release(tmp_fm_struct)
END DO ! ispin
! Copy the submatrix
IF (PRESENT(loc_coeff)) ALLOCATE (mat_ptr)
DO ispin = s_spin, l_spin
CALL get_mo_set(mos(ispin), mo_coeff=mo_coeff, &
occupation_numbers=occupations, nao=nao, nmo=nmo)
lb = localized_wfn_control%lu_bound_states(1, ispin)
ub = localized_wfn_control%lu_bound_states(2, ispin)
IF (PRESENT(loc_coeff)) THEN
mat_ptr = loc_coeff(ispin)
ELSE
mat_ptr => mo_coeff
END IF
IF ((localized_wfn_control%set_of_states == state_loc_list) .OR. &
(localized_wfn_control%set_of_states == state_loc_mixed)) THEN
ALLOCATE (vecbuffer(1, nao))
IF (localized_wfn_control%do_homo) THEN
my_occ = occupations(localized_wfn_control%loc_states(1, ispin))
END IF
nmosub = SIZE(localized_wfn_control%loc_states, 1)
CPASSERT(nmosub > 0)
imoloc = 0
DO i = lb, ub
! Get the index in the subset
imoloc = imoloc + 1
! Get the index in the full set
imo = localized_wfn_control%loc_states(i, ispin)
IF (localized_wfn_control%do_homo) THEN
occ_imo = occupations(imo)
IF (ABS(occ_imo - my_occ) > localized_wfn_control%eps_occ) THEN
IF (localized_wfn_control%localization_method /= do_loc_none) &
CALL cp_abort(__LOCATION__, &
"States with different occupations "// &
"cannot be rotated together")
END IF
END IF
! Take the imo vector from the full set and copy in the imoloc vector of the subset
CALL cp_fm_get_submatrix(mat_ptr, vecbuffer, 1, imo, &
nao, 1, transpose=.TRUE.)
CALL cp_fm_set_submatrix(moloc_coeff(ispin), vecbuffer, 1, imoloc, &
nao, 1, transpose=.TRUE.)
END DO
DEALLOCATE (vecbuffer)
ELSE
my_occ = occupations(lb)
occ_imo = occupations(ub)
IF (ABS(occ_imo - my_occ) > localized_wfn_control%eps_occ) THEN
IF (localized_wfn_control%localization_method /= do_loc_none) &
CALL cp_abort(__LOCATION__, &
"States with different occupations "// &
"cannot be rotated together")
END IF
nmosub = localized_wfn_control%nloc_states(ispin)
CALL cp_fm_to_fm(mat_ptr, moloc_coeff(ispin), nmosub, lb, 1)
END IF
! we have the mo's to be localized now, see if we can rotate them according to the history
! only do that if we have a history of course. The history is filled
IF (PRESENT(mo_loc_history)) THEN
IF (localized_wfn_control%use_history .AND. ASSOCIATED(mo_loc_history)) THEN
CALL rotate_state_to_ref(moloc_coeff(ispin), &
mo_loc_history(ispin), matrix_s(1)%matrix)
END IF
END IF
END DO
IF (PRESENT(loc_coeff)) DEALLOCATE (mat_ptr)
CALL set_qs_loc_env(qs_loc_env=qs_loc_env, cell=cell, local_molecules=local_molecules, &
moloc_coeff=moloc_coeff, particle_set=particle_set, para_env=para_env, &
localized_wfn_control=localized_wfn_control)
! Prepare the operators
NULLIFY (tmp_fm_struct, mo_coeff)
nmosub = MAXVAL(localized_wfn_control%nloc_states)
CALL get_mo_set(mos(1), mo_coeff=mo_coeff)
CALL cp_fm_struct_create(tmp_fm_struct, nrow_global=nmosub, &
ncol_global=nmosub, para_env=para_env, context=mo_coeff%matrix_struct%context)
IF (localized_wfn_control%operator_type == op_loc_berry) THEN
IF (qs_loc_env%cell%orthorhombic) THEN
dim_op = 3
ELSE
dim_op = 6
END IF
CALL set_qs_loc_env(qs_loc_env=qs_loc_env, dim_op=dim_op)
ALLOCATE (qs_loc_env%op_sm_set(2, dim_op))
DO i = 1, dim_op
DO j = 1, SIZE(qs_loc_env%op_sm_set, 1)
NULLIFY (qs_loc_env%op_sm_set(j, i)%matrix)
ALLOCATE (qs_loc_env%op_sm_set(j, i)%matrix)
CALL dbcsr_copy(qs_loc_env%op_sm_set(j, i)%matrix, matrix_s(1)%matrix, &
name="qs_loc_env%op_sm_"//TRIM(ADJUSTL(cp_to_string(j)))//"-"//TRIM(ADJUSTL(cp_to_string(i))))
CALL dbcsr_set(qs_loc_env%op_sm_set(j, i)%matrix, 0.0_dp)
END DO
END DO
ELSEIF (localized_wfn_control%operator_type == op_loc_pipek) THEN
natoms = SIZE(qs_loc_env%particle_set, 1)
ALLOCATE (qs_loc_env%op_fm_set(natoms, 1))
CALL set_qs_loc_env(qs_loc_env=qs_loc_env, dim_op=natoms)
DO ispin = 1, SIZE(qs_loc_env%op_fm_set, 2)
CALL get_mo_set(mos(ispin), nmo=nmo)
DO iatom = 1, natoms
CALL cp_fm_create(qs_loc_env%op_fm_set(iatom, ispin), tmp_fm_struct)
CALL cp_fm_get_info(qs_loc_env%op_fm_set(iatom, ispin), nrow_global=nmosub)
CPASSERT(nmo >= nmosub)
CALL cp_fm_set_all(qs_loc_env%op_fm_set(iatom, ispin), 0.0_dp)
END DO ! iatom
END DO ! ispin
ELSE
CPABORT("Type of operator not implemented")
END IF
CALL cp_fm_struct_release(tmp_fm_struct)
IF (localized_wfn_control%operator_type == op_loc_berry) THEN
CALL initialize_weights(qs_loc_env%cell, qs_loc_env%weights)
CALL get_berry_operator(qs_loc_env, qs_env)
ELSEIF (localized_wfn_control%operator_type == op_loc_pipek) THEN
!! here we don't have to do anything
!! CALL get_pipek_mezey_operator ( qs_loc_env, qs_env )
END IF
qs_loc_env%molecular_states = .FALSE.
qs_loc_env%wannier_states = .FALSE.
END IF
CALL timestop(handle)
END SUBROUTINE qs_loc_env_init
! **************************************************************************************************
!> \brief A wrapper to compute the Berry operator for periodic systems
!> \param qs_loc_env new environment for the localization calculations
!> \param qs_env the qs_env in which the qs_env lives
!> \par History
!> 04.2005 created [MI]
!> 04.2018 modified [RZK, ZL]
!> \author MI
! **************************************************************************************************
SUBROUTINE get_berry_operator(qs_loc_env, qs_env)
TYPE(qs_loc_env_type), POINTER :: qs_loc_env
TYPE(qs_environment_type), POINTER :: qs_env
CHARACTER(len=*), PARAMETER :: routineN = 'get_berry_operator'
INTEGER :: dim_op, handle
TYPE(cell_type), POINTER :: cell
TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: op_sm_set
CALL timeset(routineN, handle)
NULLIFY (cell, op_sm_set)
CALL get_qs_loc_env(qs_loc_env=qs_loc_env, op_sm_set=op_sm_set, &
cell=cell, dim_op=dim_op)
CALL compute_berry_operator(qs_env, cell, op_sm_set, dim_op)
CALL timestop(handle)
END SUBROUTINE get_berry_operator
! **************************************************************************************************
!> \brief Computes the Berry operator for periodic systems
!> used to define the spread of the MOS
!> Here the matrix elements of the type <mu|cos(kr)|nu> and <mu|sin(kr)|nu>
!> are computed, where mu and nu are the contracted basis functions.
!> Namely the Berry operator is exp(ikr)
!> k is defined somewhere
!> the pair lists are exploited and sparse matrixes are constructed
!> \param qs_env the qs_env in which the qs_env lives
!> \param cell ...
!> \param op_sm_set ...
!> \param dim_op ...
!> \par History
!> 04.2005 created [MI]
!> 04.2018 wrapped old code [RZK, ZL]
!> \author MI
!> \note
!> The intgrals are computed analytically using the primitives GTO
!> The contraction is performed block-wise
! **************************************************************************************************
SUBROUTINE compute_berry_operator(qs_env, cell, op_sm_set, dim_op)
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(cell_type), POINTER :: cell
TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: op_sm_set
INTEGER :: dim_op
CHARACTER(len=*), PARAMETER :: routineN = 'compute_berry_operator'
INTEGER :: handle, i, iatom, icol, ikind, inode, irow, iset, jatom, jkind, jset, last_jatom, &
ldab, ldsa, ldsb, ldwork, maxl, ncoa, ncob, nkind, nrow, nseta, nsetb, sgfa, sgfb
INTEGER, DIMENSION(3) :: perd0
INTEGER, DIMENSION(:), POINTER :: la_max, la_min, lb_max, lb_min, npgfa, &
npgfb, nsgfa, nsgfb
INTEGER, DIMENSION(:, :), POINTER :: first_sgfa, first_sgfb
LOGICAL :: found, new_atom_b
REAL(KIND=dp) :: dab, kvec(3), rab2, vector_k(3, 6)
REAL(KIND=dp), DIMENSION(3) :: ra, rab, rb
REAL(KIND=dp), DIMENSION(:), POINTER :: set_radius_a, set_radius_b
REAL(KIND=dp), DIMENSION(:, :), POINTER :: cosab, rpgfa, rpgfb, sinab, sphi_a, &
sphi_b, work, zeta, zetb
TYPE(block_p_type), DIMENSION(:), POINTER :: op_cos, op_sin
TYPE(gto_basis_set_p_type), DIMENSION(:), POINTER :: basis_set_list
TYPE(gto_basis_set_type), POINTER :: basis_set_a, basis_set_b
TYPE(neighbor_list_iterator_p_type), &
DIMENSION(:), POINTER :: nl_iterator
TYPE(neighbor_list_set_p_type), DIMENSION(:), &
POINTER :: sab_orb
TYPE(particle_type), DIMENSION(:), POINTER :: particle_set
TYPE(qs_kind_type), DIMENSION(:), POINTER :: qs_kind_set
TYPE(qs_kind_type), POINTER :: qs_kind
CALL timeset(routineN, handle)
NULLIFY (qs_kind, qs_kind_set)
NULLIFY (particle_set)
NULLIFY (sab_orb)
NULLIFY (cosab, sinab, work)
NULLIFY (la_max, la_min, lb_max, lb_min, npgfa, npgfb, nsgfa, nsgfb)
NULLIFY (set_radius_a, set_radius_b, rpgfa, rpgfb, sphi_a, sphi_b, zeta, zetb)
CALL get_qs_env(qs_env=qs_env, qs_kind_set=qs_kind_set, &
particle_set=particle_set, sab_orb=sab_orb)
nkind = SIZE(qs_kind_set)
CALL get_qs_kind_set(qs_kind_set=qs_kind_set, &
maxco=ldwork, maxlgto=maxl)
ldab = ldwork
ALLOCATE (cosab(ldab, ldab))
cosab = 0.0_dp
ALLOCATE (sinab(ldab, ldab))
sinab = 0.0_dp
ALLOCATE (work(ldwork, ldwork))
work = 0.0_dp
ALLOCATE (op_cos(dim_op))
ALLOCATE (op_sin(dim_op))
DO i = 1, dim_op
NULLIFY (op_cos(i)%block)
NULLIFY (op_sin(i)%block)
END DO
kvec = 0.0_dp
vector_k = 0.0_dp
vector_k(:, 1) = twopi*cell%h_inv(1, :)
vector_k(:, 2) = twopi*cell%h_inv(2, :)
vector_k(:, 3) = twopi*cell%h_inv(3, :)
vector_k(:, 4) = twopi*(cell%h_inv(1, :) + cell%h_inv(2, :))
vector_k(:, 5) = twopi*(cell%h_inv(1, :) + cell%h_inv(3, :))
vector_k(:, 6) = twopi*(cell%h_inv(2, :) + cell%h_inv(3, :))
! This operator can be used only for periodic systems
! If an isolated system is used the periodicity is overimposed
perd0(1:3) = cell%perd(1:3)
cell%perd(1:3) = 1
ALLOCATE (basis_set_list(nkind))
DO ikind = 1, nkind
qs_kind => qs_kind_set(ikind)
CALL get_qs_kind(qs_kind=qs_kind, basis_set=basis_set_a)
IF (ASSOCIATED(basis_set_a)) THEN
basis_set_list(ikind)%gto_basis_set => basis_set_a
ELSE
NULLIFY (basis_set_list(ikind)%gto_basis_set)
END IF
END DO
CALL neighbor_list_iterator_create(nl_iterator, sab_orb)
DO WHILE (neighbor_list_iterate(nl_iterator) == 0)
CALL get_iterator_info(nl_iterator, ikind=ikind, jkind=jkind, inode=inode, &
iatom=iatom, jatom=jatom, r=rab)
basis_set_a => basis_set_list(ikind)%gto_basis_set
IF (.NOT. ASSOCIATED(basis_set_a)) CYCLE
basis_set_b => basis_set_list(jkind)%gto_basis_set
IF (.NOT. ASSOCIATED(basis_set_b)) CYCLE
ra = pbc(particle_set(iatom)%r, cell)
! basis ikind
first_sgfa => basis_set_a%first_sgf
la_max => basis_set_a%lmax
la_min => basis_set_a%lmin
npgfa => basis_set_a%npgf
nseta = basis_set_a%nset
nsgfa => basis_set_a%nsgf_set
rpgfa => basis_set_a%pgf_radius
set_radius_a => basis_set_a%set_radius
sphi_a => basis_set_a%sphi
zeta => basis_set_a%zet
! basis jkind
first_sgfb => basis_set_b%first_sgf
lb_max => basis_set_b%lmax
lb_min => basis_set_b%lmin
npgfb => basis_set_b%npgf
nsetb = basis_set_b%nset
nsgfb => basis_set_b%nsgf_set
rpgfb => basis_set_b%pgf_radius
set_radius_b => basis_set_b%set_radius
sphi_b => basis_set_b%sphi
zetb => basis_set_b%zet
ldsa = SIZE(sphi_a, 1)
ldsb = SIZE(sphi_b, 1)
IF (inode == 1) last_jatom = 0
rb = rab + ra
IF (jatom /= last_jatom) THEN
new_atom_b = .TRUE.
last_jatom = jatom
ELSE
new_atom_b = .FALSE.
END IF
IF (new_atom_b) THEN
IF (iatom <= jatom) THEN
irow = iatom
icol = jatom
ELSE
irow = jatom
icol = iatom
END IF
DO i = 1, dim_op
NULLIFY (op_cos(i)%block)
CALL dbcsr_get_block_p(matrix=op_sm_set(1, i)%matrix, &
row=irow, col=icol, block=op_cos(i)%block, found=found)
NULLIFY (op_sin(i)%block)
CALL dbcsr_get_block_p(matrix=op_sm_set(2, i)%matrix, &
row=irow, col=icol, block=op_sin(i)%block, found=found)
END DO
END IF ! new_atom_b
rab2 = rab(1)*rab(1) + rab(2)*rab(2) + rab(3)*rab(3)
dab = SQRT(rab2)
nrow = 0
DO iset = 1, nseta
ncoa = npgfa(iset)*ncoset(la_max(iset))
sgfa = first_sgfa(1, iset)
DO jset = 1, nsetb
ncob = npgfb(jset)*ncoset(lb_max(jset))
sgfb = first_sgfb(1, jset)
IF (set_radius_a(iset) + set_radius_b(jset) >= dab) THEN
! *** Calculate the primitive overlap integrals ***
DO i = 1, dim_op
kvec(1:3) = vector_k(1:3, i)
cosab = 0.0_dp
sinab = 0.0_dp
CALL cossin(la_max(iset), npgfa(iset), zeta(:, iset), rpgfa(:, iset), &
la_min(iset), lb_max(jset), npgfb(jset), zetb(:, jset), &
rpgfb(:, jset), lb_min(jset), &
ra, rb, kvec, cosab, sinab)
CALL contract_cossin(op_cos(i)%block, op_sin(i)%block, &
iatom, ncoa, nsgfa(iset), sgfa, sphi_a, ldsa, &
jatom, ncob, nsgfb(jset), sgfb, sphi_b, ldsb, &
cosab, sinab, ldab, work, ldwork)
END DO
END IF ! >= dab
END DO ! jset
nrow = nrow + ncoa
END DO ! iset
END DO
CALL neighbor_list_iterator_release(nl_iterator)
! Set back the correct periodicity
cell%perd(1:3) = perd0(1:3)
DO i = 1, dim_op
NULLIFY (op_cos(i)%block)
NULLIFY (op_sin(i)%block)
END DO
DEALLOCATE (op_cos, op_sin)
DEALLOCATE (cosab, sinab, work, basis_set_list)
CALL timestop(handle)
END SUBROUTINE compute_berry_operator
! **************************************************************************************************
!> \brief ...
!> \param qs_loc_env ...
!> \param section ...
!> \param mo_array ...
!> \param coeff_localized ...
!> \param do_homo ...
!> \param evals ...
!> \param do_mixed ...
! **************************************************************************************************
SUBROUTINE loc_write_restart(qs_loc_env, section, mo_array, coeff_localized, &
do_homo, evals, do_mixed)
TYPE(qs_loc_env_type), POINTER :: qs_loc_env
TYPE(section_vals_type), POINTER :: section
TYPE(mo_set_type), DIMENSION(:), POINTER :: mo_array
TYPE(cp_fm_type), DIMENSION(:), INTENT(IN) :: coeff_localized
LOGICAL, INTENT(IN) :: do_homo
TYPE(cp_1d_r_p_type), DIMENSION(:), OPTIONAL, &
POINTER :: evals
LOGICAL, INTENT(IN), OPTIONAL :: do_mixed
CHARACTER(LEN=*), PARAMETER :: routineN = 'loc_write_restart'
CHARACTER(LEN=default_path_length) :: filename
CHARACTER(LEN=default_string_length) :: my_middle
INTEGER :: handle, ispin, max_block, nao, nloc, &
nmo, output_unit, rst_unit
LOGICAL :: my_do_mixed
TYPE(cp_logger_type), POINTER :: logger
TYPE(section_vals_type), POINTER :: print_key
CALL timeset(routineN, handle)
NULLIFY (logger)
logger => cp_get_default_logger()
output_unit = cp_logger_get_default_io_unit(logger)
IF (qs_loc_env%do_localize) THEN
print_key => section_vals_get_subs_vals(section, "LOC_RESTART")
IF (BTEST(cp_print_key_should_output(logger%iter_info, &
section, "LOC_RESTART"), &
cp_p_file)) THEN
! Open file
rst_unit = -1
my_do_mixed = .FALSE.
IF (PRESENT(do_mixed)) my_do_mixed = do_mixed
IF (do_homo) THEN
my_middle = "LOC_HOMO"
ELSEIF (my_do_mixed) THEN
my_middle = "LOC_MIXED"
ELSE
my_middle = "LOC_LUMO"
END IF
rst_unit = cp_print_key_unit_nr(logger, section, "LOC_RESTART", &
extension=".wfn", file_status="REPLACE", file_action="WRITE", &
file_form="UNFORMATTED", middle_name=TRIM(my_middle))
IF (rst_unit > 0) filename = cp_print_key_generate_filename(logger, print_key, &
middle_name=TRIM(my_middle), extension=".wfn", &
my_local=.FALSE.)
IF (output_unit > 0) THEN
WRITE (UNIT=output_unit, FMT="(/,T2,A, A/)") &
"LOCALIZATION| Write restart file for the localized MOS : ", &
TRIM(filename)
END IF
IF (rst_unit > 0) THEN
WRITE (rst_unit) qs_loc_env%localized_wfn_control%set_of_states
WRITE (rst_unit) qs_loc_env%localized_wfn_control%lu_bound_states
WRITE (rst_unit) qs_loc_env%localized_wfn_control%nloc_states
END IF
DO ispin = 1, SIZE(coeff_localized)
ASSOCIATE (mo_coeff => coeff_localized(ispin))
CALL cp_fm_get_info(mo_coeff, nrow_global=nao, ncol_global=nmo, ncol_block=max_block)
nloc = qs_loc_env%localized_wfn_control%nloc_states(ispin)
IF (rst_unit > 0) THEN
WRITE (rst_unit) qs_loc_env%localized_wfn_control%loc_states(1:nloc, ispin)
IF (do_homo .OR. my_do_mixed) THEN
WRITE (rst_unit) nmo, &
mo_array(ispin)%homo, &
mo_array(ispin)%lfomo, &
mo_array(ispin)%nelectron
WRITE (rst_unit) mo_array(ispin)%eigenvalues(1:nmo), &
mo_array(ispin)%occupation_numbers(1:nmo)
ELSE
WRITE (rst_unit) nmo
WRITE (rst_unit) evals(ispin)%array(1:nmo)
END IF
END IF
CALL cp_fm_write_unformatted(mo_coeff, rst_unit)
END ASSOCIATE
END DO
CALL cp_print_key_finished_output(rst_unit, logger, section, &
"LOC_RESTART")
END IF
END IF
CALL timestop(handle)
END SUBROUTINE loc_write_restart
! **************************************************************************************************
!> \brief ...
!> \param qs_loc_env ...
!> \param mos ...
!> \param mos_localized ...
!> \param section ...
!> \param section2 ...
!> \param para_env ...
!> \param do_homo ...
!> \param restart_found ...
!> \param evals ...
!> \param do_mixed ...
! **************************************************************************************************
SUBROUTINE loc_read_restart(qs_loc_env, mos, mos_localized, section, section2, para_env, &
do_homo, restart_found, evals, do_mixed)
TYPE(qs_loc_env_type), POINTER :: qs_loc_env
TYPE(mo_set_type), DIMENSION(:), POINTER :: mos
TYPE(cp_fm_type), DIMENSION(:), INTENT(INOUT) :: mos_localized
TYPE(section_vals_type), POINTER :: section, section2
TYPE(mp_para_env_type), POINTER :: para_env
LOGICAL, INTENT(IN) :: do_homo
LOGICAL, INTENT(INOUT) :: restart_found
TYPE(cp_1d_r_p_type), DIMENSION(:), OPTIONAL, &
POINTER :: evals
LOGICAL, INTENT(IN), OPTIONAL :: do_mixed
CHARACTER(len=*), PARAMETER :: routineN = 'loc_read_restart'
CHARACTER(LEN=25) :: fname_key
CHARACTER(LEN=default_path_length) :: filename
CHARACTER(LEN=default_string_length) :: my_middle
INTEGER :: handle, homo_read, i, ispin, lfomo_read, max_nloc, n_rep_val, nao, &
nelectron_read, nloc, nmo, nmo_read, nspin, output_unit, rst_unit
LOGICAL :: file_exists, my_do_mixed
REAL(KIND=dp), ALLOCATABLE, DIMENSION(:) :: eig_read, occ_read
REAL(KIND=dp), DIMENSION(:, :), POINTER :: vecbuffer
TYPE(cp_logger_type), POINTER :: logger
TYPE(section_vals_type), POINTER :: print_key
CALL timeset(routineN, handle)
logger => cp_get_default_logger()
nspin = SIZE(mos_localized)
nao = mos(1)%nao
rst_unit = -1
output_unit = cp_print_key_unit_nr(logger, section2, &
"PROGRAM_RUN_INFO", extension=".Log")
my_do_mixed = .FALSE.
IF (PRESENT(do_mixed)) my_do_mixed = do_mixed
IF (do_homo) THEN
fname_key = "LOCHOMO_RESTART_FILE_NAME"
ELSEIF (my_do_mixed) THEN
fname_key = "LOCMIXD_RESTART_FILE_NAME"
ELSE
fname_key = "LOCLUMO_RESTART_FILE_NAME"
IF (.NOT. PRESENT(evals)) &
CPABORT("Missing argument to localize unoccupied states.")
END IF
file_exists = .FALSE.
CALL section_vals_val_get(section, fname_key, n_rep_val=n_rep_val)
IF (n_rep_val > 0) THEN
CALL section_vals_val_get(section, fname_key, c_val=filename)
ELSE
print_key => section_vals_get_subs_vals(section2, "LOC_RESTART")
IF (do_homo) THEN
my_middle = "LOC_HOMO"
ELSEIF (my_do_mixed) THEN
my_middle = "LOC_MIXED"
ELSE
my_middle = "LOC_LUMO"
END IF
filename = cp_print_key_generate_filename(logger, print_key, &
middle_name=TRIM(my_middle), extension=".wfn", &
my_local=.FALSE.)
END IF
IF (para_env%is_source()) INQUIRE (FILE=filename, exist=file_exists)
IF (file_exists) THEN
IF (para_env%is_source()) THEN
CALL open_file(file_name=filename, &
file_action="READ", &
file_form="UNFORMATTED", &
file_status="OLD", &
unit_number=rst_unit)
READ (rst_unit) qs_loc_env%localized_wfn_control%set_of_states
READ (rst_unit) qs_loc_env%localized_wfn_control%lu_bound_states
READ (rst_unit) qs_loc_env%localized_wfn_control%nloc_states
END IF
ELSE
IF (output_unit > 0) WRITE (output_unit, "(/,T10,A)") &
"Restart file not available filename=<"//TRIM(filename)//'>'
END IF
CALL para_env%bcast(file_exists)
IF (file_exists) THEN
restart_found = .TRUE.
CALL para_env%bcast(qs_loc_env%localized_wfn_control%set_of_states)
CALL para_env%bcast(qs_loc_env%localized_wfn_control%lu_bound_states)
CALL para_env%bcast(qs_loc_env%localized_wfn_control%nloc_states)
max_nloc = MAXVAL(qs_loc_env%localized_wfn_control%nloc_states(:))
ALLOCATE (vecbuffer(1, nao))
IF (ASSOCIATED(qs_loc_env%localized_wfn_control%loc_states)) THEN
DEALLOCATE (qs_loc_env%localized_wfn_control%loc_states)
END IF
ALLOCATE (qs_loc_env%localized_wfn_control%loc_states(max_nloc, 2))
qs_loc_env%localized_wfn_control%loc_states = 0
DO ispin = 1, nspin
IF (do_homo .OR. do_mixed) THEN
nmo = mos(ispin)%nmo
ELSE
nmo = SIZE(evals(ispin)%array, 1)
END IF
IF (para_env%is_source() .AND. (nmo > 0)) THEN
nloc = qs_loc_env%localized_wfn_control%nloc_states(ispin)
READ (rst_unit) qs_loc_env%localized_wfn_control%loc_states(1:nloc, ispin)
IF (do_homo .OR. do_mixed) THEN
READ (rst_unit) nmo_read, homo_read, lfomo_read, nelectron_read
ALLOCATE (eig_read(nmo_read), occ_read(nmo_read))
eig_read = 0.0_dp
occ_read = 0.0_dp
READ (rst_unit) eig_read(1:nmo_read), occ_read(1:nmo_read)
ELSE
READ (rst_unit) nmo_read
ALLOCATE (eig_read(nmo_read))
eig_read = 0.0_dp
READ (rst_unit) eig_read(1:nmo_read)
END IF
IF (nmo_read < nmo) &
CALL cp_warn(__LOCATION__, &
"The number of MOs on the restart unit is smaller than the number of "// &
"the allocated MOs. ")
IF (nmo_read > nmo) &
CALL cp_warn(__LOCATION__, &
"The number of MOs on the restart unit is greater than the number of "// &
"the allocated MOs. The read MO set will be truncated!")
nmo = MIN(nmo, nmo_read)
IF (do_homo .OR. do_mixed) THEN
mos(ispin)%eigenvalues(1:nmo) = eig_read(1:nmo)
mos(ispin)%occupation_numbers(1:nmo) = occ_read(1:nmo)
DEALLOCATE (eig_read, occ_read)
ELSE
evals(ispin)%array(1:nmo) = eig_read(1:nmo)
DEALLOCATE (eig_read)
END IF
END IF
IF (do_homo .OR. do_mixed) THEN
CALL para_env%bcast(mos(ispin)%eigenvalues)
CALL para_env%bcast(mos(ispin)%occupation_numbers)
ELSE
CALL para_env%bcast(evals(ispin)%array)
END IF
DO i = 1, nmo
IF (para_env%is_source()) THEN
READ (rst_unit) vecbuffer
ELSE
vecbuffer(1, :) = 0.0_dp
END IF
CALL para_env%bcast(vecbuffer)
CALL cp_fm_set_submatrix(mos_localized(ispin), &
vecbuffer, 1, i, nao, 1, transpose=.TRUE.)
END DO
END DO
CALL para_env%bcast(qs_loc_env%localized_wfn_control%loc_states)
DEALLOCATE (vecbuffer)
END IF
! Close restart file
IF (para_env%is_source()) THEN
IF (file_exists) CALL close_file(unit_number=rst_unit)
END IF
CALL timestop(handle)
END SUBROUTINE loc_read_restart
! **************************************************************************************************
!> \brief initializes everything needed for localization of the HOMOs
!> \param qs_loc_env ...
!> \param loc_section ...
!> \param do_homo ...
!> \param do_mixed ...
!> \param do_xas ...
!> \param nloc_xas ...
!> \param spin_xas ...