-
Notifications
You must be signed in to change notification settings - Fork 1
/
negf_methods.F
2982 lines (2530 loc) · 149 KB
/
negf_methods.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 NEGF based quantum transport calculations
! **************************************************************************************************
MODULE negf_methods
USE bibliography, ONLY: Bailey2006,&
Papior2017,&
cite_reference
USE cp_blacs_env, ONLY: cp_blacs_env_type
USE cp_cfm_basic_linalg, ONLY: cp_cfm_scale,&
cp_cfm_scale_and_add,&
cp_cfm_trace
USE cp_cfm_types, ONLY: &
copy_cfm_info_type, cp_cfm_cleanup_copy_general, cp_cfm_create, &
cp_cfm_finish_copy_general, cp_cfm_get_info, cp_cfm_get_submatrix, cp_cfm_release, &
cp_cfm_set_submatrix, cp_cfm_start_copy_general, cp_cfm_to_fm, cp_cfm_type
USE cp_control_types, ONLY: dft_control_type
USE cp_dbcsr_api, ONLY: dbcsr_copy,&
dbcsr_deallocate_matrix,&
dbcsr_dot,&
dbcsr_init_p,&
dbcsr_p_type
USE cp_dbcsr_operations, ONLY: dbcsr_allocate_matrix_set
USE cp_fm_basic_linalg, ONLY: cp_fm_scale,&
cp_fm_scale_and_add,&
cp_fm_trace
USE cp_fm_struct, ONLY: cp_fm_struct_create,&
cp_fm_struct_release,&
cp_fm_struct_type
USE cp_fm_types, ONLY: cp_fm_copy_general,&
cp_fm_create,&
cp_fm_get_info,&
cp_fm_release,&
cp_fm_set_all,&
cp_fm_to_fm,&
cp_fm_type
USE cp_log_handling, ONLY: cp_get_default_logger,&
cp_logger_get_default_io_unit,&
cp_logger_type
USE cp_output_handling, ONLY: cp_p_file,&
cp_print_key_finished_output,&
cp_print_key_should_output,&
cp_print_key_unit_nr,&
debug_print_level,&
high_print_level
USE cp_subsys_types, ONLY: cp_subsys_type
USE force_env_types, ONLY: force_env_get,&
force_env_p_type,&
force_env_type
USE global_types, ONLY: global_environment_type
USE input_constants, ONLY: negfint_method_cc,&
negfint_method_simpson
USE input_section_types, ONLY: section_vals_get_subs_vals,&
section_vals_type,&
section_vals_val_get
USE kinds, ONLY: default_string_length,&
dp
USE kpoint_types, ONLY: get_kpoint_info,&
kpoint_type
USE machine, ONLY: m_walltime
USE mathconstants, ONLY: pi,&
twopi,&
z_one,&
z_zero
USE message_passing, ONLY: mp_para_env_type
USE negf_control_types, ONLY: negf_control_create,&
negf_control_release,&
negf_control_type,&
read_negf_control
USE negf_env_types, ONLY: negf_env_create,&
negf_env_release,&
negf_env_type
USE negf_green_cache, ONLY: green_functions_cache_expand,&
green_functions_cache_release,&
green_functions_cache_reorder,&
green_functions_cache_type
USE negf_green_methods, ONLY: do_sancho,&
negf_contact_broadening_matrix,&
negf_contact_self_energy,&
negf_retarded_green_function,&
sancho_work_matrices_create,&
sancho_work_matrices_release,&
sancho_work_matrices_type
USE negf_integr_cc, ONLY: &
cc_interval_full, cc_interval_half, cc_shape_arc, cc_shape_linear, &
ccquad_double_number_of_points, ccquad_init, ccquad_reduce_and_append_zdata, &
ccquad_refine_integral, ccquad_release, ccquad_type
USE negf_integr_simpson, ONLY: simpsonrule_get_next_nodes,&
simpsonrule_init,&
simpsonrule_refine_integral,&
simpsonrule_release,&
simpsonrule_type,&
sr_shape_arc,&
sr_shape_linear
USE negf_matrix_utils, ONLY: invert_cell_to_index,&
negf_copy_fm_submat_to_dbcsr,&
negf_copy_sym_dbcsr_to_fm_submat
USE negf_subgroup_types, ONLY: negf_sub_env_create,&
negf_sub_env_release,&
negf_subgroup_env_type
USE parallel_gemm_api, ONLY: parallel_gemm
USE physcon, ONLY: e_charge,&
evolt,&
kelvin,&
seconds
USE qs_density_mixing_types, ONLY: direct_mixing_nr,&
gspace_mixing_nr
USE qs_environment_types, ONLY: get_qs_env,&
qs_environment_type
USE qs_gspace_mixing, ONLY: gspace_mixing
USE qs_ks_methods, ONLY: qs_ks_build_kohn_sham_matrix
USE qs_mixing_utils, ONLY: mixing_allocate,&
mixing_init
USE qs_rho_methods, ONLY: qs_rho_update_rho
USE qs_rho_types, ONLY: qs_rho_get,&
qs_rho_type
USE qs_scf_methods, ONLY: scf_env_density_mixing
USE qs_subsys_types, ONLY: qs_subsys_type
USE string_utilities, ONLY: integer_to_string
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'negf_methods'
LOGICAL, PARAMETER, PRIVATE :: debug_this_module = .TRUE.
PUBLIC :: do_negf
! **************************************************************************************************
!> \brief Type to accumulate the total number of points used in integration as well as
!> the final error estimate
!> \author Sergey Chulkov
! **************************************************************************************************
TYPE integration_status_type
INTEGER :: npoints = -1
REAL(kind=dp) :: error = -1.0_dp
END TYPE integration_status_type
CONTAINS
! **************************************************************************************************
!> \brief Perform NEGF calculation.
!> \param force_env Force environment
!> \par History
!> * 01.2017 created [Sergey Chulkov]
! **************************************************************************************************
SUBROUTINE do_negf(force_env)
TYPE(force_env_type), POINTER :: force_env
CHARACTER(LEN=*), PARAMETER :: routineN = 'do_negf'
CHARACTER(len=default_string_length) :: contact_id_str
INTEGER :: handle, icontact, ispin, log_unit, &
ncontacts, npoints, nspins, &
print_level, print_unit
LOGICAL :: should_output, verbose_output
REAL(kind=dp) :: energy_max, energy_min
REAL(kind=dp), DIMENSION(2) :: current
TYPE(cp_blacs_env_type), POINTER :: blacs_env
TYPE(cp_logger_type), POINTER :: logger
TYPE(cp_subsys_type), POINTER :: cp_subsys
TYPE(dft_control_type), POINTER :: dft_control
TYPE(force_env_p_type), DIMENSION(:), POINTER :: sub_force_env
TYPE(global_environment_type), POINTER :: global_env
TYPE(negf_control_type), POINTER :: negf_control
TYPE(negf_env_type) :: negf_env
TYPE(negf_subgroup_env_type) :: sub_env
TYPE(qs_environment_type), POINTER :: qs_env
TYPE(section_vals_type), POINTER :: negf_contact_section, &
negf_mixing_section, negf_section, &
print_section, root_section
CALL timeset(routineN, handle)
logger => cp_get_default_logger()
log_unit = cp_logger_get_default_io_unit()
CALL cite_reference(Bailey2006)
CALL cite_reference(Papior2017)
NULLIFY (blacs_env, cp_subsys, global_env, qs_env, root_section, sub_force_env)
CALL force_env_get(force_env, globenv=global_env, qs_env=qs_env, root_section=root_section, &
sub_force_env=sub_force_env, subsys=cp_subsys)
CALL get_qs_env(qs_env, blacs_env=blacs_env)
negf_section => section_vals_get_subs_vals(root_section, "NEGF")
negf_contact_section => section_vals_get_subs_vals(negf_section, "CONTACT")
negf_mixing_section => section_vals_get_subs_vals(negf_section, "MIXING")
NULLIFY (negf_control)
CALL negf_control_create(negf_control)
CALL read_negf_control(negf_control, root_section, cp_subsys)
! print unit, if log_unit > 0, otherwise no output
log_unit = cp_print_key_unit_nr(logger, negf_section, "PRINT%PROGRAM_RUN_INFO", extension=".Log")
! print levels, are used if log_unit > 0
IF (log_unit > 0) THEN
CALL section_vals_val_get(negf_section, "PRINT%PROGRAM_RUN_INFO%PRINT_LEVEL", i_val=print_level)
SELECT CASE (print_level)
CASE (high_print_level, debug_print_level)
verbose_output = .TRUE.
CASE DEFAULT
verbose_output = .FALSE.
END SELECT
END IF
IF (log_unit > 0) THEN
WRITE (log_unit, '(/,T2,A,T62)') "COMPUTE THE RELEVANT HAMILTONIAN MATRICES"
END IF
CALL negf_sub_env_create(sub_env, negf_control, blacs_env, global_env%blacs_grid_layout, global_env%blacs_repeatable)
CALL negf_env_create(negf_env, sub_env, negf_control, force_env, negf_mixing_section, log_unit)
IF (log_unit > 0 .AND. verbose_output) THEN
DO icontact = 1, SIZE(negf_control%contacts)
WRITE (log_unit, "(/,' NEGF| Atoms in the contact region',I2,':',I4)") &
icontact, SIZE(negf_control%contacts(icontact)%atomlist_bulk)
WRITE (log_unit, "(16I5)") negf_control%contacts(icontact)%atomlist_bulk
END DO
WRITE (log_unit, "(/,' NEGF| Atoms in the full scattering region:',I4)") SIZE(negf_control%atomlist_S_screening)
WRITE (log_unit, "(16I5)") negf_control%atomlist_S_screening
WRITE (log_unit, *)
END IF
! compute contact Fermi level as well as requested properties
ncontacts = SIZE(negf_control%contacts)
DO icontact = 1, ncontacts
NULLIFY (qs_env)
IF (negf_control%contacts(icontact)%force_env_index > 0) THEN
CALL force_env_get(sub_force_env(negf_control%contacts(icontact)%force_env_index)%force_env, qs_env=qs_env)
ELSE
CALL force_env_get(force_env, qs_env=qs_env)
END IF
CALL guess_fermi_level(icontact, negf_env, negf_control, sub_env, qs_env, log_unit)
print_section => section_vals_get_subs_vals(negf_contact_section, "PRINT", i_rep_section=icontact)
should_output = BTEST(cp_print_key_should_output(logger%iter_info, print_section, "DOS"), cp_p_file)
IF (should_output) THEN
CALL section_vals_val_get(print_section, "DOS%FROM_ENERGY", r_val=energy_min)
CALL section_vals_val_get(print_section, "DOS%TILL_ENERGY", r_val=energy_max)
CALL section_vals_val_get(print_section, "DOS%N_GRIDPOINTS", i_val=npoints)
CALL integer_to_string(icontact, contact_id_str)
print_unit = cp_print_key_unit_nr(logger, print_section, "DOS", &
extension=".dos", &
middle_name=TRIM(ADJUSTL(contact_id_str)), &
file_status="REPLACE")
CALL negf_print_dos(print_unit, energy_min, energy_max, npoints, &
v_shift=0.0_dp, negf_env=negf_env, negf_control=negf_control, &
sub_env=sub_env, base_contact=icontact, just_contact=icontact)
CALL cp_print_key_finished_output(print_unit, logger, print_section, "DOS")
END IF
END DO
IF (ncontacts > 1) THEN
CALL force_env_get(force_env, qs_env=qs_env)
CALL shift_potential(negf_env, negf_control, sub_env, qs_env, base_contact=1, log_unit=log_unit)
CALL converge_density(negf_env, negf_control, sub_env, qs_env, negf_control%v_shift, base_contact=1, log_unit=log_unit)
! current
CALL get_qs_env(qs_env, dft_control=dft_control)
nspins = dft_control%nspins
CPASSERT(nspins <= 2)
DO ispin = 1, nspins
! compute the electric current flown through a pair of electrodes
! contact_id1 -> extended molecule -> contact_id2.
! Only extended systems with two electrodes are supported at the moment,
! so for the time being the contacts' indices are hardcoded.
current(ispin) = negf_compute_current(contact_id1=1, contact_id2=2, &
v_shift=negf_control%v_shift, &
negf_env=negf_env, &
negf_control=negf_control, &
sub_env=sub_env, &
ispin=ispin, &
blacs_env_global=blacs_env)
END DO
IF (log_unit > 0) THEN
IF (nspins > 1) THEN
WRITE (log_unit, '(/,T2,A,T60,ES20.7E2)') "NEGF| Alpha-spin electric current (A)", current(1)
WRITE (log_unit, '(T2,A,T60,ES20.7E2)') "NEGF| Beta-spin electric current (A)", current(2)
ELSE
WRITE (log_unit, '(/,T2,A,T60,ES20.7E2)') "NEGF| Electric current (A)", 2.0_dp*current(1)
END IF
END IF
! density of states
print_section => section_vals_get_subs_vals(negf_section, "PRINT")
should_output = BTEST(cp_print_key_should_output(logger%iter_info, print_section, "DOS"), cp_p_file)
IF (should_output) THEN
CALL section_vals_val_get(print_section, "DOS%FROM_ENERGY", r_val=energy_min)
CALL section_vals_val_get(print_section, "DOS%TILL_ENERGY", r_val=energy_max)
CALL section_vals_val_get(print_section, "DOS%N_GRIDPOINTS", i_val=npoints)
CALL integer_to_string(0, contact_id_str)
print_unit = cp_print_key_unit_nr(logger, print_section, "DOS", &
extension=".dos", &
middle_name=TRIM(ADJUSTL(contact_id_str)), &
file_status="REPLACE")
CALL negf_print_dos(print_unit, energy_min, energy_max, npoints, negf_control%v_shift, &
negf_env=negf_env, negf_control=negf_control, &
sub_env=sub_env, base_contact=1)
CALL cp_print_key_finished_output(print_unit, logger, print_section, "DOS")
END IF
! transmission coefficient
should_output = BTEST(cp_print_key_should_output(logger%iter_info, print_section, "TRANSMISSION"), cp_p_file)
IF (should_output) THEN
CALL section_vals_val_get(print_section, "TRANSMISSION%FROM_ENERGY", r_val=energy_min)
CALL section_vals_val_get(print_section, "TRANSMISSION%TILL_ENERGY", r_val=energy_max)
CALL section_vals_val_get(print_section, "TRANSMISSION%N_GRIDPOINTS", i_val=npoints)
CALL integer_to_string(0, contact_id_str)
print_unit = cp_print_key_unit_nr(logger, print_section, "TRANSMISSION", &
extension=".transm", &
middle_name=TRIM(ADJUSTL(contact_id_str)), &
file_status="REPLACE")
CALL negf_print_transmission(print_unit, energy_min, energy_max, npoints, negf_control%v_shift, &
negf_env=negf_env, negf_control=negf_control, &
sub_env=sub_env, contact_id1=1, contact_id2=2)
CALL cp_print_key_finished_output(print_unit, logger, print_section, "TRANSMISSION")
END IF
END IF
CALL negf_env_release(negf_env)
CALL negf_sub_env_release(sub_env)
CALL negf_control_release(negf_control)
CALL timestop(handle)
END SUBROUTINE do_negf
! **************************************************************************************************
!> \brief Compute the contact's Fermi level.
!> \param contact_id index of the contact
!> \param negf_env NEGF environment
!> \param negf_control NEGF control
!> \param sub_env NEGF parallel (sub)group environment
!> \param qs_env QuickStep environment
!> \param log_unit output unit
!> \par History
!> * 10.2017 created [Sergey Chulkov]
! **************************************************************************************************
SUBROUTINE guess_fermi_level(contact_id, negf_env, negf_control, sub_env, qs_env, log_unit)
INTEGER, INTENT(in) :: contact_id
TYPE(negf_env_type), INTENT(in) :: negf_env
TYPE(negf_control_type), POINTER :: negf_control
TYPE(negf_subgroup_env_type), INTENT(in) :: sub_env
TYPE(qs_environment_type), POINTER :: qs_env
INTEGER, INTENT(in) :: log_unit
CHARACTER(LEN=*), PARAMETER :: routineN = 'guess_fermi_level'
TYPE(cp_fm_type), PARAMETER :: fm_dummy = cp_fm_type()
CHARACTER(len=default_string_length) :: temperature_str
COMPLEX(kind=dp) :: lbound_cpath, lbound_lpath, ubound_lpath
INTEGER :: direction_axis_abs, handle, image, &
ispin, nao, nimages, nspins, step
INTEGER, ALLOCATABLE, DIMENSION(:, :) :: index_to_cell
INTEGER, DIMENSION(:, :, :), POINTER :: cell_to_index
LOGICAL :: do_kpoints
REAL(kind=dp) :: delta_au, energy_ubound_minus_fermi, fermi_level_guess, fermi_level_max, &
fermi_level_min, nelectrons_guess, nelectrons_max, nelectrons_min, nelectrons_qs_cell0, &
nelectrons_qs_cell1, offset_au, rscale, t1, t2, trace
TYPE(cp_blacs_env_type), POINTER :: blacs_env_global
TYPE(cp_fm_struct_type), POINTER :: fm_struct
TYPE(cp_fm_type) :: rho_ao_fm
TYPE(cp_fm_type), POINTER :: matrix_s_fm
TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_s_kp, rho_ao_qs_kp
TYPE(dft_control_type), POINTER :: dft_control
TYPE(green_functions_cache_type) :: g_surf_cache
TYPE(integration_status_type) :: stats
TYPE(kpoint_type), POINTER :: kpoints
TYPE(mp_para_env_type), POINTER :: para_env_global
TYPE(qs_rho_type), POINTER :: rho_struct
TYPE(qs_subsys_type), POINTER :: subsys
CALL timeset(routineN, handle)
IF (negf_control%contacts(contact_id)%compute_fermi_level) THEN
CALL get_qs_env(qs_env, &
blacs_env=blacs_env_global, &
dft_control=dft_control, &
do_kpoints=do_kpoints, &
kpoints=kpoints, &
matrix_s_kp=matrix_s_kp, &
para_env=para_env_global, &
rho=rho_struct, subsys=subsys)
CALL qs_rho_get(rho_struct, rho_ao_kp=rho_ao_qs_kp)
nimages = dft_control%nimages
nspins = dft_control%nspins
direction_axis_abs = ABS(negf_env%contacts(contact_id)%direction_axis)
CPASSERT(SIZE(negf_env%contacts(contact_id)%h_00) == nspins)
IF (sub_env%ngroups > 1) THEN
NULLIFY (matrix_s_fm, fm_struct)
CALL cp_fm_get_info(negf_env%contacts(contact_id)%s_00, nrow_global=nao)
CALL cp_fm_struct_create(fm_struct, nrow_global=nao, ncol_global=nao, context=blacs_env_global)
CALL cp_fm_create(rho_ao_fm, fm_struct)
ALLOCATE (matrix_s_fm)
CALL cp_fm_create(matrix_s_fm, fm_struct)
CALL cp_fm_struct_release(fm_struct)
IF (sub_env%group_distribution(sub_env%mepos_global) == 0) THEN
CALL cp_fm_copy_general(negf_env%contacts(contact_id)%s_00, matrix_s_fm, para_env_global)
ELSE
CALL cp_fm_copy_general(fm_dummy, matrix_s_fm, para_env_global)
END IF
ELSE
matrix_s_fm => negf_env%contacts(contact_id)%s_00
CALL cp_fm_get_info(matrix_s_fm, matrix_struct=fm_struct)
CALL cp_fm_create(rho_ao_fm, fm_struct)
END IF
IF (do_kpoints) THEN
CALL get_kpoint_info(kpoints, cell_to_index=cell_to_index)
ELSE
ALLOCATE (cell_to_index(0:0, 0:0, 0:0))
cell_to_index(0, 0, 0) = 1
END IF
ALLOCATE (index_to_cell(3, nimages))
CALL invert_cell_to_index(cell_to_index, nimages, index_to_cell)
IF (.NOT. do_kpoints) DEALLOCATE (cell_to_index)
IF (nspins == 1) THEN
! spin-restricted calculation: number of electrons must be doubled
rscale = 2.0_dp
ELSE
rscale = 1.0_dp
END IF
! compute the refence number of electrons using the electron density
nelectrons_qs_cell0 = 0.0_dp
nelectrons_qs_cell1 = 0.0_dp
DO image = 1, nimages
IF (index_to_cell(direction_axis_abs, image) == 0) THEN
DO ispin = 1, nspins
CALL dbcsr_dot(rho_ao_qs_kp(ispin, image)%matrix, matrix_s_kp(1, image)%matrix, trace)
nelectrons_qs_cell0 = nelectrons_qs_cell0 + trace
END DO
ELSE IF (ABS(index_to_cell(direction_axis_abs, image)) == 1) THEN
DO ispin = 1, nspins
CALL dbcsr_dot(rho_ao_qs_kp(ispin, image)%matrix, matrix_s_kp(1, image)%matrix, trace)
nelectrons_qs_cell1 = nelectrons_qs_cell1 + trace
END DO
END IF
END DO
DEALLOCATE (index_to_cell)
IF (log_unit > 0) THEN
WRITE (temperature_str, '(F11.3)') negf_control%contacts(contact_id)%temperature*kelvin
WRITE (log_unit, '(/,T2,A,I0,A)') "COMPUTE FERMI LEVEL OF CONTACT ", &
contact_id, " AT "//TRIM(ADJUSTL(temperature_str))//" KELVIN"
WRITE (log_unit, '(/,T2,A,T60,F20.10,/)') "Electronic density of the isolated contact unit cell:", &
-1.0_dp*(nelectrons_qs_cell0 + nelectrons_qs_cell1)
WRITE (log_unit, '(T3,A)') "Step Integration method Time Fermi level Convergence (density)"
WRITE (log_unit, '(T3,78("-"))')
END IF
nelectrons_qs_cell0 = 0.0_dp
DO ispin = 1, nspins
CALL cp_fm_trace(negf_env%contacts(contact_id)%rho_00(ispin), &
negf_env%contacts(contact_id)%s_00, trace)
nelectrons_qs_cell0 = nelectrons_qs_cell0 + trace
END DO
! Use orbital energies of HOMO and LUMO as reference points and then
! refine the Fermi level by using a simple linear interpolation technique
IF (negf_control%homo_lumo_gap > 0.0_dp) THEN
IF (negf_control%contacts(contact_id)%refine_fermi_level) THEN
fermi_level_min = negf_control%contacts(contact_id)%fermi_level
ELSE
fermi_level_min = negf_env%contacts(contact_id)%homo_energy
END IF
fermi_level_max = fermi_level_min + negf_control%homo_lumo_gap
ELSE
IF (negf_control%contacts(contact_id)%refine_fermi_level) THEN
fermi_level_max = negf_control%contacts(contact_id)%fermi_level
ELSE
fermi_level_max = negf_env%contacts(contact_id)%homo_energy
END IF
fermi_level_min = fermi_level_max + negf_control%homo_lumo_gap
END IF
step = 0
lbound_cpath = CMPLX(negf_control%energy_lbound, negf_control%eta, kind=dp)
delta_au = REAL(negf_control%delta_npoles, kind=dp)*twopi*negf_control%contacts(contact_id)%temperature
offset_au = REAL(negf_control%gamma_kT, kind=dp)*negf_control%contacts(contact_id)%temperature
energy_ubound_minus_fermi = -2.0_dp*LOG(negf_control%conv_density)*negf_control%contacts(contact_id)%temperature
t1 = m_walltime()
DO
step = step + 1
SELECT CASE (step)
CASE (1)
fermi_level_guess = fermi_level_min
CASE (2)
fermi_level_guess = fermi_level_max
CASE DEFAULT
fermi_level_guess = fermi_level_min - (nelectrons_min - nelectrons_qs_cell0)* &
(fermi_level_max - fermi_level_min)/(nelectrons_max - nelectrons_min)
END SELECT
negf_control%contacts(contact_id)%fermi_level = fermi_level_guess
nelectrons_guess = 0.0_dp
lbound_lpath = CMPLX(fermi_level_guess - offset_au, delta_au, kind=dp)
ubound_lpath = CMPLX(fermi_level_guess + energy_ubound_minus_fermi, delta_au, kind=dp)
CALL integration_status_reset(stats)
DO ispin = 1, nspins
CALL negf_init_rho_equiv_residuals(rho_ao_fm=rho_ao_fm, &
v_shift=0.0_dp, &
ignore_bias=.TRUE., &
negf_env=negf_env, &
negf_control=negf_control, &
sub_env=sub_env, &
ispin=ispin, &
base_contact=contact_id, &
just_contact=contact_id)
CALL negf_add_rho_equiv_low(rho_ao_fm=rho_ao_fm, &
stats=stats, &
v_shift=0.0_dp, &
ignore_bias=.TRUE., &
negf_env=negf_env, &
negf_control=negf_control, &
sub_env=sub_env, &
ispin=ispin, &
base_contact=contact_id, &
integr_lbound=lbound_cpath, &
integr_ubound=lbound_lpath, &
matrix_s_global=matrix_s_fm, &
is_circular=.TRUE., &
g_surf_cache=g_surf_cache, &
just_contact=contact_id)
CALL green_functions_cache_release(g_surf_cache)
CALL negf_add_rho_equiv_low(rho_ao_fm=rho_ao_fm, &
stats=stats, &
v_shift=0.0_dp, &
ignore_bias=.TRUE., &
negf_env=negf_env, &
negf_control=negf_control, &
sub_env=sub_env, &
ispin=ispin, &
base_contact=contact_id, &
integr_lbound=lbound_lpath, &
integr_ubound=ubound_lpath, &
matrix_s_global=matrix_s_fm, &
is_circular=.FALSE., &
g_surf_cache=g_surf_cache, &
just_contact=contact_id)
CALL green_functions_cache_release(g_surf_cache)
CALL cp_fm_trace(rho_ao_fm, matrix_s_fm, trace)
nelectrons_guess = nelectrons_guess + trace
END DO
nelectrons_guess = nelectrons_guess*rscale
t2 = m_walltime()
IF (log_unit > 0) THEN
WRITE (log_unit, '(T2,I5,T12,A,T32,F8.1,T42,F15.8,T60,ES20.5E2)') &
step, get_method_description_string(stats, negf_control%integr_method), &
t2 - t1, fermi_level_guess, nelectrons_guess - nelectrons_qs_cell0
END IF
IF (ABS(nelectrons_qs_cell0 - nelectrons_guess) < negf_control%conv_density) EXIT
SELECT CASE (step)
CASE (1)
nelectrons_min = nelectrons_guess
CASE (2)
nelectrons_max = nelectrons_guess
CASE DEFAULT
IF (fermi_level_guess < fermi_level_min) THEN
fermi_level_max = fermi_level_min
nelectrons_max = nelectrons_min
fermi_level_min = fermi_level_guess
nelectrons_min = nelectrons_guess
ELSE IF (fermi_level_guess > fermi_level_max) THEN
fermi_level_min = fermi_level_max
nelectrons_min = nelectrons_max
fermi_level_max = fermi_level_guess
nelectrons_max = nelectrons_guess
ELSE IF (fermi_level_max - fermi_level_guess < fermi_level_guess - fermi_level_min) THEN
fermi_level_max = fermi_level_guess
nelectrons_max = nelectrons_guess
ELSE
fermi_level_min = fermi_level_guess
nelectrons_min = nelectrons_guess
END IF
END SELECT
t1 = t2
END DO
negf_control%contacts(contact_id)%fermi_level = fermi_level_guess
IF (sub_env%ngroups > 1) THEN
CALL cp_fm_release(matrix_s_fm)
DEALLOCATE (matrix_s_fm)
END IF
CALL cp_fm_release(rho_ao_fm)
END IF
IF (log_unit > 0) THEN
WRITE (temperature_str, '(F11.3)') negf_control%contacts(contact_id)%temperature*kelvin
WRITE (log_unit, '(/,T2,A,I0)') "NEGF| Contact No. ", contact_id
WRITE (log_unit, '(T2,A,T62,F18.8)') "NEGF| Fermi level at "//TRIM(ADJUSTL(temperature_str))// &
" Kelvin (a.u.):", negf_control%contacts(contact_id)%fermi_level
WRITE (log_unit, '(T2,A,T62,F18.8)') "NEGF| Electric potential (V):", &
negf_control%contacts(contact_id)%v_external*evolt
END IF
CALL timestop(handle)
END SUBROUTINE guess_fermi_level
! **************************************************************************************************
!> \brief Compute shift in Hartree potential
!> \param negf_env NEGF environment
!> \param negf_control NEGF control
!> \param sub_env NEGF parallel (sub)group environment
!> \param qs_env QuickStep environment
!> \param base_contact index of the reference contact
!> \param log_unit output unit
! **************************************************************************************************
SUBROUTINE shift_potential(negf_env, negf_control, sub_env, qs_env, base_contact, log_unit)
TYPE(negf_env_type), INTENT(in) :: negf_env
TYPE(negf_control_type), POINTER :: negf_control
TYPE(negf_subgroup_env_type), INTENT(in) :: sub_env
TYPE(qs_environment_type), POINTER :: qs_env
INTEGER, INTENT(in) :: base_contact, log_unit
CHARACTER(LEN=*), PARAMETER :: routineN = 'shift_potential'
TYPE(cp_fm_type), PARAMETER :: fm_dummy = cp_fm_type()
COMPLEX(kind=dp) :: lbound_cpath, ubound_cpath, ubound_lpath
INTEGER :: handle, ispin, iter_count, nao, &
ncontacts, nspins
LOGICAL :: do_kpoints
REAL(kind=dp) :: mu_base, nelectrons_guess, nelectrons_max, nelectrons_min, nelectrons_ref, &
t1, t2, temperature, trace, v_shift_guess, v_shift_max, v_shift_min
TYPE(cp_blacs_env_type), POINTER :: blacs_env
TYPE(cp_fm_struct_type), POINTER :: fm_struct
TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:) :: rho_ao_fm
TYPE(cp_fm_type), POINTER :: matrix_s_fm
TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: rho_ao_qs_kp
TYPE(dft_control_type), POINTER :: dft_control
TYPE(green_functions_cache_type), ALLOCATABLE, &
DIMENSION(:) :: g_surf_circular, g_surf_linear
TYPE(integration_status_type) :: stats
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(qs_rho_type), POINTER :: rho_struct
TYPE(qs_subsys_type), POINTER :: subsys
ncontacts = SIZE(negf_control%contacts)
! nothing to do
IF (.NOT. (ALLOCATED(negf_env%h_s) .AND. ALLOCATED(negf_env%h_sc) .AND. &
ASSOCIATED(negf_env%s_s) .AND. ALLOCATED(negf_env%s_sc))) RETURN
IF (ncontacts < 2) RETURN
CALL timeset(routineN, handle)
CALL get_qs_env(qs_env, blacs_env=blacs_env, do_kpoints=do_kpoints, dft_control=dft_control, &
para_env=para_env, rho=rho_struct, subsys=subsys)
CPASSERT(.NOT. do_kpoints)
! apply external NEGF potential
t1 = m_walltime()
! need a globally distributed overlap matrix in order to compute integration errors
IF (sub_env%ngroups > 1) THEN
NULLIFY (matrix_s_fm, fm_struct)
CALL cp_fm_get_info(negf_env%s_s, nrow_global=nao)
CALL cp_fm_struct_create(fm_struct, nrow_global=nao, ncol_global=nao, context=blacs_env)
ALLOCATE (matrix_s_fm)
CALL cp_fm_create(matrix_s_fm, fm_struct)
CALL cp_fm_struct_release(fm_struct)
IF (sub_env%group_distribution(sub_env%mepos_global) == 0) THEN
CALL cp_fm_copy_general(negf_env%s_s, matrix_s_fm, para_env)
ELSE
CALL cp_fm_copy_general(fm_dummy, matrix_s_fm, para_env)
END IF
ELSE
matrix_s_fm => negf_env%s_s
END IF
CALL cp_fm_get_info(matrix_s_fm, matrix_struct=fm_struct)
nspins = SIZE(negf_env%h_s)
mu_base = negf_control%contacts(base_contact)%fermi_level
! keep the initial charge density matrix and Kohn-Sham matrix
CALL qs_rho_get(rho_struct, rho_ao_kp=rho_ao_qs_kp)
! extract the reference density matrix blocks
nelectrons_ref = 0.0_dp
ALLOCATE (rho_ao_fm(nspins))
DO ispin = 1, nspins
CALL cp_fm_create(rho_ao_fm(ispin), fm_struct)
CALL negf_copy_sym_dbcsr_to_fm_submat(matrix=rho_ao_qs_kp(ispin, 1)%matrix, &
fm=rho_ao_fm(ispin), &
atomlist_row=negf_control%atomlist_S_screening, &
atomlist_col=negf_control%atomlist_S_screening, &
subsys=subsys, mpi_comm_global=para_env, &
do_upper_diag=.TRUE., do_lower=.TRUE.)
CALL cp_fm_trace(rho_ao_fm(ispin), matrix_s_fm, trace)
nelectrons_ref = nelectrons_ref + trace
END DO
IF (log_unit > 0) THEN
WRITE (log_unit, '(/,T2,A)') "COMPUTE SHIFT IN HARTREE POTENTIAL"
WRITE (log_unit, '(/,T2,A,T55,F25.14,/)') "Initial electronic density of the scattering region:", -1.0_dp*nelectrons_ref
WRITE (log_unit, '(T3,A)') "Step Integration method Time V shift Convergence (density)"
WRITE (log_unit, '(T3,78("-"))')
END IF
temperature = negf_control%contacts(base_contact)%temperature
! integration limits: C-path (arch)
lbound_cpath = CMPLX(negf_control%energy_lbound, negf_control%eta, kind=dp)
ubound_cpath = CMPLX(mu_base - REAL(negf_control%gamma_kT, kind=dp)*temperature, &
REAL(negf_control%delta_npoles, kind=dp)*twopi*temperature, kind=dp)
! integration limits: L-path (linear)
ubound_lpath = CMPLX(mu_base - LOG(negf_control%conv_density)*temperature, &
REAL(negf_control%delta_npoles, kind=dp)*twopi*temperature, kind=dp)
v_shift_min = negf_control%v_shift
v_shift_max = negf_control%v_shift + negf_control%v_shift_offset
ALLOCATE (g_surf_circular(nspins), g_surf_linear(nspins))
DO iter_count = 1, negf_control%v_shift_maxiters
SELECT CASE (iter_count)
CASE (1)
v_shift_guess = v_shift_min
CASE (2)
v_shift_guess = v_shift_max
CASE DEFAULT
v_shift_guess = v_shift_min - (nelectrons_min - nelectrons_ref)* &
(v_shift_max - v_shift_min)/(nelectrons_max - nelectrons_min)
END SELECT
! compute an updated density matrix
CALL integration_status_reset(stats)
DO ispin = 1, nspins
! closed contour: residuals
CALL negf_init_rho_equiv_residuals(rho_ao_fm=rho_ao_fm(ispin), &
v_shift=v_shift_guess, &
ignore_bias=.TRUE., &
negf_env=negf_env, &
negf_control=negf_control, &
sub_env=sub_env, &
ispin=ispin, &
base_contact=base_contact)
! closed contour: C-path
CALL negf_add_rho_equiv_low(rho_ao_fm=rho_ao_fm(ispin), &
stats=stats, &
v_shift=v_shift_guess, &
ignore_bias=.TRUE., &
negf_env=negf_env, &
negf_control=negf_control, &
sub_env=sub_env, &
ispin=ispin, &
base_contact=base_contact, &
integr_lbound=lbound_cpath, &
integr_ubound=ubound_cpath, &
matrix_s_global=matrix_s_fm, &
is_circular=.TRUE., &
g_surf_cache=g_surf_circular(ispin))
IF (negf_control%disable_cache) &
CALL green_functions_cache_release(g_surf_circular(ispin))
! closed contour: L-path
CALL negf_add_rho_equiv_low(rho_ao_fm=rho_ao_fm(ispin), &
stats=stats, &
v_shift=v_shift_guess, &
ignore_bias=.TRUE., &
negf_env=negf_env, &
negf_control=negf_control, &
sub_env=sub_env, &
ispin=ispin, &
base_contact=base_contact, &
integr_lbound=ubound_cpath, &
integr_ubound=ubound_lpath, &
matrix_s_global=matrix_s_fm, &
is_circular=.FALSE., &
g_surf_cache=g_surf_linear(ispin))
IF (negf_control%disable_cache) &
CALL green_functions_cache_release(g_surf_linear(ispin))
END DO
IF (nspins > 1) THEN
DO ispin = 2, nspins
CALL cp_fm_scale_and_add(1.0_dp, rho_ao_fm(1), 1.0_dp, rho_ao_fm(ispin))
END DO
ELSE
CALL cp_fm_scale(2.0_dp, rho_ao_fm(1))
END IF
CALL cp_fm_trace(rho_ao_fm(1), matrix_s_fm, nelectrons_guess)
t2 = m_walltime()
IF (log_unit > 0) THEN
WRITE (log_unit, '(T2,I5,T12,A,T32,F8.1,T42,F15.8,T60,ES20.5E2)') &
iter_count, get_method_description_string(stats, negf_control%integr_method), &
t2 - t1, v_shift_guess, nelectrons_guess - nelectrons_ref
END IF
IF (ABS(nelectrons_guess - nelectrons_ref) < negf_control%conv_scf) EXIT
! compute correction
SELECT CASE (iter_count)
CASE (1)
nelectrons_min = nelectrons_guess
CASE (2)
nelectrons_max = nelectrons_guess
CASE DEFAULT
IF (v_shift_guess < v_shift_min) THEN
v_shift_max = v_shift_min
nelectrons_max = nelectrons_min
v_shift_min = v_shift_guess
nelectrons_min = nelectrons_guess
ELSE IF (v_shift_guess > v_shift_max) THEN
v_shift_min = v_shift_max
nelectrons_min = nelectrons_max
v_shift_max = v_shift_guess
nelectrons_max = nelectrons_guess
ELSE IF (v_shift_max - v_shift_guess < v_shift_guess - v_shift_min) THEN
v_shift_max = v_shift_guess
nelectrons_max = nelectrons_guess
ELSE
v_shift_min = v_shift_guess
nelectrons_min = nelectrons_guess
END IF
END SELECT
t1 = t2
END DO
negf_control%v_shift = v_shift_guess
IF (log_unit > 0) THEN
WRITE (log_unit, '(T2,A,T62,F18.8)') "NEGF| Shift in Hartree potential", negf_control%v_shift
END IF
DO ispin = nspins, 1, -1
CALL green_functions_cache_release(g_surf_circular(ispin))
CALL green_functions_cache_release(g_surf_linear(ispin))
END DO
DEALLOCATE (g_surf_circular, g_surf_linear)
CALL cp_fm_release(rho_ao_fm)
IF (sub_env%ngroups > 1 .AND. ASSOCIATED(matrix_s_fm)) THEN
CALL cp_fm_release(matrix_s_fm)
DEALLOCATE (matrix_s_fm)
END IF
CALL timestop(handle)
END SUBROUTINE shift_potential
! **************************************************************************************************
!> \brief Converge electronic density of the scattering region.
!> \param negf_env NEGF environment
!> \param negf_control NEGF control
!> \param sub_env NEGF parallel (sub)group environment
!> \param qs_env QuickStep environment
!> \param v_shift shift in Hartree potential
!> \param base_contact index of the reference contact
!> \param log_unit output unit
!> \par History
!> * 06.2017 created [Sergey Chulkov]
! **************************************************************************************************
SUBROUTINE converge_density(negf_env, negf_control, sub_env, qs_env, v_shift, base_contact, log_unit)
TYPE(negf_env_type), INTENT(in) :: negf_env
TYPE(negf_control_type), POINTER :: negf_control
TYPE(negf_subgroup_env_type), INTENT(in) :: sub_env
TYPE(qs_environment_type), POINTER :: qs_env
REAL(kind=dp), INTENT(in) :: v_shift
INTEGER, INTENT(in) :: base_contact, log_unit
CHARACTER(LEN=*), PARAMETER :: routineN = 'converge_density'
REAL(kind=dp), PARAMETER :: threshold = 16.0_dp*EPSILON(0.0_dp)
TYPE(cp_fm_type), PARAMETER :: fm_dummy = cp_fm_type()
COMPLEX(kind=dp) :: lbound_cpath, ubound_cpath, ubound_lpath
INTEGER :: handle, icontact, image, ispin, &
iter_count, nao, ncontacts, nimages, &
nspins
LOGICAL :: do_kpoints
REAL(kind=dp) :: iter_delta, mu_base, nelectrons, &
nelectrons_diff, t1, t2, temperature, &
trace, v_base, v_contact
TYPE(cp_blacs_env_type), POINTER :: blacs_env
TYPE(cp_fm_struct_type), POINTER :: fm_struct
TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:) :: rho_ao_delta_fm, rho_ao_new_fm
TYPE(cp_fm_type), POINTER :: matrix_s_fm
TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER :: matrix_ks_initial_kp, matrix_ks_qs_kp, &
rho_ao_initial_kp, rho_ao_new_kp, &
rho_ao_qs_kp
TYPE(dft_control_type), POINTER :: dft_control
TYPE(green_functions_cache_type), ALLOCATABLE, &
DIMENSION(:) :: g_surf_circular, g_surf_linear, &
g_surf_nonequiv
TYPE(integration_status_type) :: stats
TYPE(mp_para_env_type), POINTER :: para_env
TYPE(qs_rho_type), POINTER :: rho_struct
TYPE(qs_subsys_type), POINTER :: subsys
ncontacts = SIZE(negf_control%contacts)
! nothing to do
IF (.NOT. (ALLOCATED(negf_env%h_s) .AND. ALLOCATED(negf_env%h_sc) .AND. &
ASSOCIATED(negf_env%s_s) .AND. ALLOCATED(negf_env%s_sc))) RETURN
IF (ncontacts < 2) RETURN
CALL timeset(routineN, handle)
CALL get_qs_env(qs_env, blacs_env=blacs_env, do_kpoints=do_kpoints, dft_control=dft_control, &
matrix_ks_kp=matrix_ks_qs_kp, para_env=para_env, rho=rho_struct, subsys=subsys)
CPASSERT(.NOT. do_kpoints)
! apply external NEGF potential
t1 = m_walltime()
! need a globally distributed overlap matrix in order to compute integration errors
IF (sub_env%ngroups > 1) THEN
NULLIFY (matrix_s_fm, fm_struct)
CALL cp_fm_get_info(negf_env%s_s, nrow_global=nao)
CALL cp_fm_struct_create(fm_struct, nrow_global=nao, ncol_global=nao, context=blacs_env)
ALLOCATE (matrix_s_fm)
CALL cp_fm_create(matrix_s_fm, fm_struct)
CALL cp_fm_struct_release(fm_struct)
IF (sub_env%group_distribution(sub_env%mepos_global) == 0) THEN
CALL cp_fm_copy_general(negf_env%s_s, matrix_s_fm, para_env)
ELSE
CALL cp_fm_copy_general(fm_dummy, matrix_s_fm, para_env)
END IF
ELSE
matrix_s_fm => negf_env%s_s
END IF
CALL cp_fm_get_info(matrix_s_fm, matrix_struct=fm_struct)
nspins = SIZE(negf_env%h_s)
nimages = dft_control%nimages
v_base = negf_control%contacts(base_contact)%v_external
mu_base = negf_control%contacts(base_contact)%fermi_level + v_base
! the current subroutine works for the general case as well, but the Poisson solver does not
IF (ncontacts > 2) THEN
CPABORT("Poisson solver does not support the general NEGF setup (>2 contacts).")
END IF
! keep the initial charge density matrix and Kohn-Sham matrix
CALL qs_rho_get(rho_struct, rho_ao_kp=rho_ao_qs_kp)