LCOV - code coverage report
Current view: top level - src - negf_env_types.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:3db43b4) Lines: 54.1 % 764 413
Test Date: 2026-04-03 06:55:30 Functions: 60.0 % 20 12

            Line data    Source code
       1              : !--------------------------------------------------------------------------------------------------!
       2              : !   CP2K: A general program to perform molecular dynamics simulations                              !
       3              : !   Copyright 2000-2026 CP2K developers group <https://cp2k.org>                                   !
       4              : !                                                                                                  !
       5              : !   SPDX-License-Identifier: GPL-2.0-or-later                                                      !
       6              : !--------------------------------------------------------------------------------------------------!
       7              : 
       8              : ! **************************************************************************************************
       9              : !> \brief Environment for NEGF based quantum transport calculations
      10              : ! **************************************************************************************************
      11              : MODULE negf_env_types
      12              :    USE cell_types,                      ONLY: cell_type,&
      13              :                                               real_to_scaled
      14              :    USE cp_blacs_env,                    ONLY: cp_blacs_env_type
      15              :    USE cp_control_types,                ONLY: dft_control_type
      16              :    USE cp_dbcsr_api,                    ONLY: dbcsr_copy,&
      17              :                                               dbcsr_deallocate_matrix,&
      18              :                                               dbcsr_init_p,&
      19              :                                               dbcsr_p_type,&
      20              :                                               dbcsr_set
      21              :    USE cp_files,                        ONLY: close_file,&
      22              :                                               open_file
      23              :    USE cp_fm_struct,                    ONLY: cp_fm_struct_create,&
      24              :                                               cp_fm_struct_release,&
      25              :                                               cp_fm_struct_type
      26              :    USE cp_fm_types,                     ONLY: cp_fm_create,&
      27              :                                               cp_fm_get_info,&
      28              :                                               cp_fm_get_submatrix,&
      29              :                                               cp_fm_release,&
      30              :                                               cp_fm_set_submatrix,&
      31              :                                               cp_fm_type
      32              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      33              :                                               cp_logger_type
      34              :    USE force_env_types,                 ONLY: force_env_get,&
      35              :                                               force_env_p_type,&
      36              :                                               force_env_type,&
      37              :                                               use_qs_force
      38              :    USE input_section_types,             ONLY: section_vals_get_subs_vals,&
      39              :                                               section_vals_type,&
      40              :                                               section_vals_val_get
      41              :    USE kinds,                           ONLY: default_path_length,&
      42              :                                               default_string_length,&
      43              :                                               dp
      44              :    USE kpoint_types,                    ONLY: get_kpoint_env,&
      45              :                                               get_kpoint_info,&
      46              :                                               kpoint_env_p_type,&
      47              :                                               kpoint_type
      48              :    USE message_passing,                 ONLY: mp_para_env_type
      49              :    USE negf_atom_map,                   ONLY: negf_atom_map_type,&
      50              :                                               negf_map_atomic_indices
      51              :    USE negf_control_types,              ONLY: negf_control_contact_type,&
      52              :                                               negf_control_type
      53              :    USE negf_io,                         ONLY: negf_print_matrix_to_file,&
      54              :                                               negf_read_matrix_from_file,&
      55              :                                               negf_restart_file_name
      56              :    USE negf_matrix_utils,               ONLY: invert_cell_to_index,&
      57              :                                               negf_copy_contact_matrix,&
      58              :                                               negf_copy_sym_dbcsr_to_fm_submat,&
      59              :                                               number_of_atomic_orbitals
      60              :    USE negf_subgroup_types,             ONLY: negf_subgroup_env_type
      61              :    USE negf_vectors,                    ONLY: contact_direction_vector,&
      62              :                                               projection_on_direction_vector
      63              :    USE particle_types,                  ONLY: particle_type
      64              :    USE pw_env_types,                    ONLY: pw_env_get,&
      65              :                                               pw_env_type
      66              :    USE pw_pool_types,                   ONLY: pw_pool_type
      67              :    USE pw_types,                        ONLY: pw_r3d_rs_type
      68              :    USE qs_density_mixing_types,         ONLY: mixing_storage_create,&
      69              :                                               mixing_storage_release,&
      70              :                                               mixing_storage_type
      71              :    USE qs_energy,                       ONLY: qs_energies
      72              :    USE qs_energy_init,                  ONLY: qs_energies_init
      73              :    USE qs_environment_types,            ONLY: get_qs_env,&
      74              :                                               qs_environment_type
      75              :    USE qs_integrate_potential,          ONLY: integrate_v_rspace
      76              :    USE qs_mo_types,                     ONLY: get_mo_set,&
      77              :                                               mo_set_type
      78              :    USE qs_rho_types,                    ONLY: qs_rho_get,&
      79              :                                               qs_rho_type
      80              :    USE qs_subsys_types,                 ONLY: qs_subsys_get,&
      81              :                                               qs_subsys_type
      82              : #include "./base/base_uses.f90"
      83              : 
      84              :    IMPLICIT NONE
      85              :    PRIVATE
      86              : 
      87              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'negf_env_types'
      88              :    LOGICAL, PARAMETER, PRIVATE          :: debug_this_module = .TRUE.
      89              : 
      90              :    PUBLIC :: negf_env_type, negf_env_contact_type
      91              :    PUBLIC :: negf_env_create, negf_env_release
      92              : 
      93              : ! **************************************************************************************************
      94              : !> \brief  Contact-specific NEGF environment.
      95              : !> \author Sergey Chulkov
      96              : ! **************************************************************************************************
      97              :    TYPE negf_env_contact_type
      98              :       REAL(kind=dp), DIMENSION(3)                        :: direction_vector = -1.0_dp, origin = -1.0_dp
      99              :       REAL(kind=dp), DIMENSION(3)                        :: direction_vector_bias = -1.0_dp, origin_bias = -1.0_dp
     100              :       !> an axis towards the secondary contact unit cell which coincides with the transport direction
     101              :       !> 0 (undefined), 1 (+x), 2 (+y), 3 (+z), -1 (-x), -2 (-y), -3 (-z)
     102              :       INTEGER                                            :: direction_axis = -1
     103              :       !> atoms belonging to a primary contact unit cell
     104              :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atomlist_cell0
     105              :       !> atoms belonging to a secondary contact unit cell (will be removed one day ...)
     106              :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atomlist_cell1
     107              :       !> list of equivalent atoms in an appropriate contact force environment
     108              :       TYPE(negf_atom_map_type), ALLOCATABLE, &
     109              :          DIMENSION(:)                                    :: atom_map_cell0, atom_map_cell1
     110              :       !> Fermi energy
     111              :       REAL(kind=dp)                                      :: fermi_energy = 0.0_dp
     112              :       !> energy of the HOMO
     113              :       REAL(kind=dp)                                      :: homo_energy = -1.0_dp
     114              :       !> number of electrons Sp(rho_00,s_00)
     115              :       REAL(kind=dp)                                      :: nelectrons_qs_cell0 = 0.0_dp
     116              :       !> number of electrons Sp(rho_01,s_01)
     117              :       REAL(kind=dp)                                      :: nelectrons_qs_cell1 = 0.0_dp
     118              :       !> diagonal (h_00) and off-diagonal (h_01) blocks of the contact Kohn-Sham matrix ([number_of_spins]).
     119              :       !> The matrix h_01 is of the shape [nao_cell0 x nao_cell1]
     120              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: h_00, h_01
     121              :       !> diagonal and off-diagonal blocks of the density matrix
     122              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)        :: rho_00, rho_01
     123              :       !> diagonal and off-diagonal blocks of the overlap matrix
     124              :       TYPE(cp_fm_type), POINTER                          :: s_00 => null(), s_01 => null()
     125              :    END TYPE negf_env_contact_type
     126              : 
     127              : ! **************************************************************************************************
     128              : !> \brief  NEGF environment.
     129              : !> \author Sergey Chulkov
     130              : ! **************************************************************************************************
     131              :    TYPE negf_env_type
     132              :       !> contact-specific NEGF environments
     133              :       TYPE(negf_env_contact_type), ALLOCATABLE, &
     134              :          DIMENSION(:)                                   :: contacts
     135              :       !> Kohn-Sham matrix of the scattering region
     136              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)       :: h_s
     137              :       !> Kohn-Sham matrix of the scattering region -- contact interface ([nspins, ncontacts])
     138              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:, :)    :: h_sc
     139              :       !> overlap matrix of the scattering region
     140              :       TYPE(cp_fm_type), POINTER                         :: s_s => null()
     141              :       !> an external Hartree potential in atomic basis set representation
     142              :       TYPE(cp_fm_type), POINTER                         :: v_hartree_s => null()
     143              :       !> overlap matrix of the scattering region -- contact interface for every contact ([ncontacts])
     144              :       TYPE(cp_fm_type), ALLOCATABLE, DIMENSION(:)       :: s_sc
     145              :       !> structure needed for density mixing
     146              :       TYPE(mixing_storage_type), POINTER                :: mixing_storage => NULL()
     147              :       !> density mixing method
     148              :       INTEGER                                           :: mixing_method = -1
     149              :       !> number of electrons Sp(rho_s,s_s)
     150              :       REAL(kind=dp)                                     :: nelectrons_ref = 0.0_dp
     151              :       !> number of electrons Sp(rho_s,s_s)
     152              :       REAL(kind=dp)                                     :: nelectrons = 0.0_dp
     153              :    END TYPE negf_env_type
     154              : 
     155              : ! **************************************************************************************************
     156              : !> \brief  Allocatable list of the type 'negf_atom_map_type'.
     157              : !> \author Sergey Chulkov
     158              : ! **************************************************************************************************
     159              :    TYPE negf_atom_map_contact_type
     160              :       TYPE(negf_atom_map_type), ALLOCATABLE, DIMENSION(:) :: atom_map
     161              :    END TYPE negf_atom_map_contact_type
     162              : 
     163              : CONTAINS
     164              : 
     165              : ! **************************************************************************************************
     166              : !> \brief Create a new NEGF environment and compute the relevant Kohn-Sham matrices.
     167              : !> \param negf_env            NEGF environment to create
     168              : !> \param sub_env             NEGF parallel (sub)group environment
     169              : !> \param negf_control        NEGF control
     170              : !> \param force_env           the primary force environment
     171              : !> \param negf_mixing_section pointer to a mixing section within the NEGF input section
     172              : !> \param log_unit            output unit number
     173              : !> \par History
     174              : !>   * 01.2017 created [Sergey Chulkov]
     175              : ! **************************************************************************************************
     176            4 :    SUBROUTINE negf_env_create(negf_env, sub_env, negf_control, force_env, negf_mixing_section, log_unit)
     177              :       TYPE(negf_env_type), INTENT(inout)                 :: negf_env
     178              :       TYPE(negf_subgroup_env_type), INTENT(in)           :: sub_env
     179              :       TYPE(negf_control_type), POINTER                   :: negf_control
     180              :       TYPE(force_env_type), POINTER                      :: force_env
     181              :       TYPE(section_vals_type), POINTER                   :: negf_mixing_section
     182              :       INTEGER, INTENT(in)                                :: log_unit
     183              : 
     184              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'negf_env_create'
     185              : 
     186              :       CHARACTER(len=default_string_length)               :: contact_str, force_env_str, &
     187              :                                                             n_force_env_str
     188              :       INTEGER                                            :: handle, icontact, in_use, n_force_env, &
     189              :                                                             ncontacts
     190              :       LOGICAL                                            :: do_kpoints, is_dft_entire
     191              :       TYPE(cp_blacs_env_type), POINTER                   :: blacs_env
     192            4 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_ks_kp, matrix_s_kp
     193              :       TYPE(dft_control_type), POINTER                    :: dft_control
     194            4 :       TYPE(force_env_p_type), DIMENSION(:), POINTER      :: sub_force_env
     195              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     196              :       TYPE(negf_atom_map_contact_type), ALLOCATABLE, &
     197            4 :          DIMENSION(:)                                    :: map_contact
     198              :       TYPE(pw_r3d_rs_type), POINTER                      :: v_hartree_rspace
     199              :       TYPE(qs_environment_type), POINTER                 :: qs_env, qs_env_contact
     200              :       TYPE(qs_subsys_type), POINTER                      :: subsys, subsys_contact
     201              :       TYPE(section_vals_type), POINTER                   :: negf_section, root_section
     202              : 
     203            4 :       CALL timeset(routineN, handle)
     204              : 
     205              :       ! ensure we have Quickstep enabled for all force_env
     206            4 :       NULLIFY (sub_force_env)
     207              :       CALL force_env_get(force_env, in_use=in_use, qs_env=qs_env, root_section=root_section, &
     208            4 :                          sub_force_env=sub_force_env)
     209              : 
     210            4 :       IF (ASSOCIATED(sub_force_env)) THEN
     211            2 :          n_force_env = SIZE(sub_force_env)
     212              :       ELSE
     213            2 :          n_force_env = 0
     214              :       END IF
     215              : 
     216            4 :       IF (in_use == use_qs_force) THEN
     217            8 :          DO icontact = 1, n_force_env
     218            4 :             CALL force_env_get(sub_force_env(icontact)%force_env, in_use=in_use)
     219            8 :             IF (in_use /= use_qs_force) EXIT
     220              :          END DO
     221              :       END IF
     222              : 
     223            4 :       IF (in_use /= use_qs_force) THEN
     224            0 :          CPABORT("Quickstep is required for NEGF run.")
     225              :       END IF
     226              : 
     227              :       ! check that all mentioned FORCE_EVAL sections are actually present
     228            4 :       ncontacts = SIZE(negf_control%contacts)
     229              : 
     230           12 :       DO icontact = 1, ncontacts
     231           12 :          IF (negf_control%contacts(icontact)%force_env_index > n_force_env) THEN
     232            0 :             WRITE (contact_str, '(I11)') icontact
     233            0 :             WRITE (force_env_str, '(I11)') negf_control%contacts(icontact)%force_env_index
     234            0 :             WRITE (n_force_env_str, '(I11)') n_force_env
     235              : 
     236              :             CALL cp_abort(__LOCATION__, &
     237              :                           "Contact number "//TRIM(ADJUSTL(contact_str))//" is linked with the FORCE_EVAL section number "// &
     238              :                           TRIM(ADJUSTL(force_env_str))//", however only "//TRIM(ADJUSTL(n_force_env_str))// &
     239              :                           " FORCE_EVAL sections have been found. Note that FORCE_EVAL sections are enumerated from 0"// &
     240            0 :                           " and that the primary (0-th) section must contain all the atoms.")
     241              :          END IF
     242              :       END DO
     243              : 
     244              :       ! create basic matrices and neighbour lists for the primary force_env,
     245              :       ! so we know how matrix elements are actually distributed across CPUs.
     246            4 :       CALL qs_energies_init(qs_env, calc_forces=.FALSE.)
     247              :       CALL get_qs_env(qs_env, blacs_env=blacs_env, do_kpoints=do_kpoints, &
     248              :                       matrix_s_kp=matrix_s_kp, matrix_ks_kp=matrix_ks_kp, &
     249            4 :                       para_env=para_env, subsys=subsys, v_hartree_rspace=v_hartree_rspace)
     250              : 
     251            4 :       negf_section => section_vals_get_subs_vals(root_section, "NEGF")
     252              : 
     253            4 :       IF (do_kpoints) THEN
     254            0 :          CPABORT("k-points are currently not supported for device FORCE_EVAL")
     255              :       END IF
     256              : 
     257              :       ! stage 1: map the atoms between the device force_env and all contact force_env-s
     258           80 :       ALLOCATE (negf_env%contacts(ncontacts))
     259           20 :       ALLOCATE (map_contact(ncontacts))
     260              : 
     261           12 :       DO icontact = 1, ncontacts
     262           12 :          IF (negf_control%contacts(icontact)%force_env_index > 0) THEN
     263            4 :             CALL force_env_get(sub_force_env(negf_control%contacts(icontact)%force_env_index)%force_env, qs_env=qs_env_contact)
     264            4 :             CALL get_qs_env(qs_env_contact, subsys=subsys_contact)
     265              : 
     266              :             CALL negf_env_contact_init_maps(contact_env=negf_env%contacts(icontact), &
     267              :                                             contact_control=negf_control%contacts(icontact), &
     268              :                                             atom_map=map_contact(icontact)%atom_map, &
     269              :                                             eps_geometry=negf_control%eps_geometry, &
     270              :                                             subsys_device=subsys, &
     271            4 :                                             subsys_contact=subsys_contact)
     272              : 
     273            4 :             IF (negf_env%contacts(icontact)%direction_axis == 0) THEN
     274            0 :                WRITE (contact_str, '(I11)') icontact
     275            0 :                WRITE (force_env_str, '(I11)') negf_control%contacts(icontact)%force_env_index
     276              :                CALL cp_abort(__LOCATION__, &
     277              :                              "One lattice vector of the contact unit cell (FORCE_EVAL section "// &
     278              :                              TRIM(ADJUSTL(force_env_str))//") must be parallel to the direction of the contact "// &
     279            0 :                              TRIM(ADJUSTL(contact_str))//".")
     280              :             END IF
     281              :          END IF
     282              :       END DO
     283              : 
     284              :       ! stage 2: obtain relevant Kohn-Sham matrix blocks for each contact (separate bulk DFT calculation)
     285           12 :       DO icontact = 1, ncontacts
     286           12 :          IF (negf_control%contacts(icontact)%force_env_index > 0) THEN
     287            4 :             IF (negf_control%contacts(icontact)%read_write_HS) THEN
     288              :                CALL negf_env_contact_read_write_hs &
     289              :                   (icontact, sub_force_env(negf_control%contacts(icontact)%force_env_index)%force_env, &
     290            0 :                    para_env, negf_env, sub_env, negf_control, negf_section, log_unit, is_separate=.TRUE.)
     291              :             ELSE
     292            4 :                IF (log_unit > 0) &
     293            2 :                   WRITE (log_unit, '(/,T2,A,T70,I11,/,A)') "NEGF| Construct the Kohn-Sham matrix for the contact", icontact, &
     294            4 :                   "       from the separate bulk DFT calculation"
     295            4 :                CALL force_env_get(sub_force_env(negf_control%contacts(icontact)%force_env_index)%force_env, qs_env=qs_env_contact)
     296            4 :                CALL qs_energies(qs_env_contact, consistent_energies=.FALSE., calc_forces=.FALSE.)
     297              :                CALL negf_env_contact_init_matrices(contact_env=negf_env%contacts(icontact), sub_env=sub_env, &
     298            4 :                                                    qs_env_contact=qs_env_contact)
     299            4 :                IF (log_unit > 0) WRITE (log_unit, '(/,T2,79("-"))')
     300              :             END IF
     301              :          END IF
     302              :       END DO
     303              : 
     304              :       ! *** obtain relevant Kohn-Sham matrix blocks for each contact with no separate FORCE_ENV ***
     305            4 :       is_dft_entire = .FALSE.
     306           12 :       DO icontact = 1, ncontacts
     307           12 :          IF (negf_control%contacts(icontact)%force_env_index <= 0) THEN
     308            4 :             IF (negf_control%contacts(icontact)%read_write_HS) THEN
     309              :                CALL negf_env_contact_init_matrices_gamma(contact_env=negf_env%contacts(icontact), &
     310              :                                                          contact_control=negf_control%contacts(icontact), &
     311              :                                                          sub_env=sub_env, qs_env=qs_env, &
     312            0 :                                                          eps_geometry=negf_control%eps_geometry)
     313              :                CALL negf_env_contact_read_write_hs(icontact, force_env, para_env, negf_env, sub_env, negf_control, negf_section, &
     314            0 :                                                    log_unit, is_separate=.FALSE., is_dft_entire=is_dft_entire)
     315              :             ELSE
     316            4 :                IF (log_unit > 0) &
     317            2 :                   WRITE (log_unit, '(/,T2,A,T70,I11,/,A)') "NEGF| Construct the Kohn-Sham matrix for the contact", icontact, &
     318            4 :                   "       from the entire system bulk DFT calculation"
     319            4 :                IF (.NOT. is_dft_entire) CALL qs_energies(qs_env, consistent_energies=.FALSE., calc_forces=.FALSE.)
     320            4 :                is_dft_entire = .TRUE.
     321              :                CALL negf_env_contact_init_matrices_gamma(contact_env=negf_env%contacts(icontact), &
     322              :                                                          contact_control=negf_control%contacts(icontact), &
     323              :                                                          sub_env=sub_env, qs_env=qs_env, &
     324            4 :                                                          eps_geometry=negf_control%eps_geometry)
     325            4 :                IF (log_unit > 0) WRITE (log_unit, '(/,T2,79("-"))')
     326              :             END IF
     327              :          END IF
     328              :       END DO
     329              : 
     330              :       ! stage 3:  obtain an initial KS-matrix for the scattering region
     331            4 :       IF (log_unit > 0) &
     332            2 :          WRITE (log_unit, '(/,T2,A,T70)') "NEGF| Construct the Kohn-Sham matrix for the scattering region"
     333            4 :       IF (negf_control%read_write_HS) THEN
     334              :          CALL negf_env_scatt_read_write_hs(force_env, para_env, negf_env, sub_env, negf_control, negf_section, log_unit, &
     335            0 :                                            is_dft_entire=is_dft_entire)
     336              :       ELSE
     337            4 :          IF (.NOT. is_dft_entire) THEN
     338            2 :             CALL qs_energies(qs_env, consistent_energies=.FALSE., calc_forces=.FALSE.)
     339            2 :             is_dft_entire = .TRUE.
     340              :          END IF
     341              :          ! extract device-related matrix blocks
     342            4 :          CALL negf_env_device_init_matrices(negf_env, negf_control, sub_env, qs_env)
     343              :       END IF
     344            4 :       IF (log_unit > 0) WRITE (log_unit, '(/,T2,79("-"))')
     345              : 
     346            4 :       negf_control%is_dft_entire = is_dft_entire
     347              : 
     348              :       ! electron density mixing;
     349              :       ! the input section below should be consistent with the subroutine create_negf_section()
     350            4 :       NULLIFY (negf_env%mixing_storage)
     351            4 :       CALL section_vals_val_get(negf_mixing_section, "METHOD", i_val=negf_env%mixing_method)
     352              : 
     353            4 :       CALL get_qs_env(qs_env, dft_control=dft_control)
     354           16 :       ALLOCATE (negf_env%mixing_storage)
     355              :       CALL mixing_storage_create(negf_env%mixing_storage, negf_mixing_section, &
     356            4 :                                  negf_env%mixing_method, dft_control%qs_control%cutoff)
     357              : 
     358            4 :       CALL timestop(handle)
     359           16 :    END SUBROUTINE negf_env_create
     360              : 
     361              : ! **************************************************************************************************
     362              : !> \brief Establish mapping between the primary and the contact force environments
     363              : !> \param contact_env         NEGF environment for the given contact (modified on exit)
     364              : !> \param contact_control     NEGF control
     365              : !> \param atom_map            atomic map
     366              : !> \param eps_geometry        accuracy in mapping atoms between different force environments
     367              : !> \param subsys_device       QuickStep subsystem of the device force environment
     368              : !> \param subsys_contact      QuickStep subsystem of the contact force environment
     369              : !> \author Sergey Chulkov
     370              : ! **************************************************************************************************
     371            4 :    SUBROUTINE negf_env_contact_init_maps(contact_env, contact_control, atom_map, &
     372              :                                          eps_geometry, subsys_device, subsys_contact)
     373              :       TYPE(negf_env_contact_type), INTENT(inout)         :: contact_env
     374              :       TYPE(negf_control_contact_type), INTENT(in)        :: contact_control
     375              :       TYPE(negf_atom_map_type), ALLOCATABLE, &
     376              :          DIMENSION(:), INTENT(inout)                     :: atom_map
     377              :       REAL(kind=dp), INTENT(in)                          :: eps_geometry
     378              :       TYPE(qs_subsys_type), POINTER                      :: subsys_device, subsys_contact
     379              : 
     380              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'negf_env_contact_init_maps'
     381              : 
     382              :       INTEGER                                            :: handle, natoms
     383              : 
     384            4 :       CALL timeset(routineN, handle)
     385              : 
     386              :       CALL contact_direction_vector(contact_env%origin, &
     387              :                                     contact_env%direction_vector, &
     388              :                                     contact_env%origin_bias, &
     389              :                                     contact_env%direction_vector_bias, &
     390              :                                     contact_control%atomlist_screening, &
     391              :                                     contact_control%atomlist_bulk, &
     392            4 :                                     subsys_device)
     393              : 
     394            4 :       contact_env%direction_axis = contact_direction_axis(contact_env%direction_vector, subsys_contact, eps_geometry)
     395              : 
     396            4 :       IF (contact_env%direction_axis /= 0) THEN
     397            4 :          natoms = SIZE(contact_control%atomlist_bulk)
     398           56 :          ALLOCATE (atom_map(natoms))
     399              : 
     400              :          ! map atom listed in 'contact_control%atomlist_bulk' to the corresponding atom/cell replica from the contact force_env
     401              :          CALL negf_map_atomic_indices(atom_map=atom_map, &
     402              :                                       atom_list=contact_control%atomlist_bulk, &
     403              :                                       subsys_device=subsys_device, &
     404              :                                       subsys_contact=subsys_contact, &
     405            4 :                                       eps_geometry=eps_geometry)
     406              : 
     407              :          ! list atoms from 'contact_control%atomlist_bulk' which belong to
     408              :          ! the primary unit cell of the bulk region for the given contact
     409              :          CALL list_atoms_in_bulk_primary_unit_cell(atomlist_cell0=contact_env%atomlist_cell0, &
     410              :                                                    atom_map_cell0=contact_env%atom_map_cell0, &
     411              :                                                    atomlist_bulk=contact_control%atomlist_bulk, &
     412              :                                                    atom_map=atom_map, &
     413              :                                                    origin=contact_env%origin, &
     414              :                                                    direction_vector=contact_env%direction_vector, &
     415              :                                                    direction_axis=contact_env%direction_axis, &
     416            4 :                                                    subsys_device=subsys_device)
     417              : 
     418              :          ! secondary unit cell
     419              :          CALL list_atoms_in_bulk_secondary_unit_cell(atomlist_cell1=contact_env%atomlist_cell1, &
     420              :                                                      atom_map_cell1=contact_env%atom_map_cell1, &
     421              :                                                      atomlist_bulk=contact_control%atomlist_bulk, &
     422              :                                                      atom_map=atom_map, &
     423              :                                                      origin=contact_env%origin, &
     424              :                                                      direction_vector=contact_env%direction_vector, &
     425              :                                                      direction_axis=contact_env%direction_axis, &
     426            4 :                                                      subsys_device=subsys_device)
     427              :       END IF
     428              : 
     429            4 :       CALL timestop(handle)
     430            4 :    END SUBROUTINE negf_env_contact_init_maps
     431              : 
     432              : ! **************************************************************************************************
     433              : !> \brief Reading and writing of the electrode Hamiltonian and overlap matrices from/to a file.
     434              : !> \param icontact     ...
     435              : !> \param el_force_env ...
     436              : !> \param para_env     ...
     437              : !> \param negf_env     ...
     438              : !> \param sub_env      ...
     439              : !> \param negf_control ...
     440              : !> \param negf_section ...
     441              : !> \param log_unit     ...
     442              : !> \param is_separate  ...
     443              : !> \param is_dft_entire ...
     444              : !> \par History
     445              : !>    * 12.2025 created  [Dmitry Ryndyk]
     446              : ! **************************************************************************************************
     447            0 :    SUBROUTINE negf_env_contact_read_write_hs(icontact, el_force_env, para_env, negf_env, sub_env, negf_control, &
     448              :                                              negf_section, log_unit, is_separate, is_dft_entire)
     449              :       INTEGER                                            :: icontact
     450              :       TYPE(force_env_type), POINTER                      :: el_force_env
     451              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     452              :       TYPE(negf_env_type), INTENT(inout)                 :: negf_env
     453              :       TYPE(negf_subgroup_env_type), INTENT(in)           :: sub_env
     454              :       TYPE(negf_control_type), POINTER                   :: negf_control
     455              :       TYPE(section_vals_type), POINTER                   :: negf_section
     456              :       INTEGER, INTENT(in)                                :: log_unit
     457              :       LOGICAL, INTENT(in)                                :: is_separate
     458              :       LOGICAL, INTENT(inout), OPTIONAL                   :: is_dft_entire
     459              : 
     460              :       CHARACTER(len=*), PARAMETER :: routineN = 'negf_env_contact_read_write_hs'
     461              : 
     462              :       CHARACTER(len=default_path_length)                 :: filename_h00_1, filename_h00_2, &
     463              :                                                             filename_h01_1, filename_h01_2, &
     464              :                                                             filename_s00, filename_s01
     465              :       INTEGER                                            :: handle, ispin, ncol, nrow, nspins, &
     466              :                                                             print_unit
     467              :       LOGICAL                                            :: exist, exist_all
     468            0 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: target_m
     469              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct
     470              :       TYPE(cp_logger_type), POINTER                      :: logger
     471              :       TYPE(dft_control_type), POINTER                    :: dft_control
     472              :       TYPE(qs_environment_type), POINTER                 :: qs_env_contact
     473              :       TYPE(qs_subsys_type), POINTER                      :: subsys
     474              : 
     475            0 :       CALL timeset(routineN, handle)
     476            0 :       logger => cp_get_default_logger()
     477              : 
     478            0 :       CALL force_env_get(el_force_env, qs_env=qs_env_contact)
     479            0 :       CALL get_qs_env(qs_env_contact, dft_control=dft_control, subsys=subsys)
     480            0 :       nspins = dft_control%nspins
     481              : 
     482            0 :       IF (log_unit > 0) WRITE (log_unit, '(/,T2,A,T70,I11)') &
     483            0 :          "NEGF| Construct the Kohn-Sham matrix for the contact", icontact
     484              : 
     485              :       ! Check that the files exist.
     486              :       ! ispin=0 is used to show nspins=1
     487            0 :       exist_all = .TRUE.
     488            0 :       IF (para_env%is_source()) THEN
     489            0 :          CALL negf_restart_file_name(filename_s00, exist, negf_section, logger, icontact, s00=.TRUE.)
     490            0 :          IF (.NOT. exist) THEN
     491              :             CALL cp_warn(__LOCATION__, &
     492              :                          "User requested to read the overlap matrix from the file named: "// &
     493            0 :                          TRIM(filename_s00)//". This file does not exist. The file will be created.")
     494            0 :             exist_all = .FALSE.
     495              :          END IF
     496            0 :          CALL negf_restart_file_name(filename_s01, exist, negf_section, logger, icontact, s01=.TRUE.)
     497            0 :          IF (.NOT. exist) THEN
     498              :             CALL cp_warn(__LOCATION__, &
     499              :                          "User requested to read the overlap matrix from the file named: "// &
     500            0 :                          TRIM(filename_s01)//". This file does not exist. The file will be created.")
     501            0 :             exist_all = .FALSE.
     502              :          END IF
     503            0 :          IF (nspins == 1) THEN
     504            0 :             CALL negf_restart_file_name(filename_h00_1, exist, negf_section, logger, icontact, ispin=0, h00=.TRUE.)
     505            0 :             IF (.NOT. exist) THEN
     506              :                CALL cp_warn(__LOCATION__, &
     507              :                             "User requested to read the Hamiltonian matrix from the file named: "// &
     508            0 :                             TRIM(filename_h00_1)//". This file does not exist. The file will be created.")
     509            0 :                exist_all = .FALSE.
     510              :             END IF
     511            0 :             CALL negf_restart_file_name(filename_h01_1, exist, negf_section, logger, icontact, ispin=0, h01=.TRUE.)
     512            0 :             IF (.NOT. exist) THEN
     513              :                CALL cp_warn(__LOCATION__, &
     514              :                             "User requested to read the Hamiltonian matrix from the file named: "// &
     515            0 :                             TRIM(filename_h01_1)//". This file does not exist. The file will be created.")
     516            0 :                exist_all = .FALSE.
     517              :             END IF
     518              :          END IF
     519            0 :          IF (nspins == 2) THEN
     520            0 :             CALL negf_restart_file_name(filename_h00_1, exist, negf_section, logger, icontact, ispin=1, h00=.TRUE.)
     521            0 :             IF (.NOT. exist) THEN
     522              :                CALL cp_warn(__LOCATION__, &
     523              :                             "User requested to read the Hamiltonian matrix from the file named: "// &
     524            0 :                             TRIM(filename_h00_1)//". This file does not exist. The file will be created.")
     525            0 :                exist_all = .FALSE.
     526              :             END IF
     527            0 :             CALL negf_restart_file_name(filename_h01_1, exist, negf_section, logger, icontact, ispin=1, h01=.TRUE.)
     528            0 :             IF (.NOT. exist) THEN
     529              :                CALL cp_warn(__LOCATION__, &
     530              :                             "User requested to read tthe Hamiltonian matrix from the file named: "// &
     531            0 :                             TRIM(filename_h01_1)//". This file does not exist. The file will be created.")
     532            0 :                exist_all = .FALSE.
     533              :             END IF
     534            0 :             CALL negf_restart_file_name(filename_h00_2, exist, negf_section, logger, icontact, ispin=2, h00=.TRUE.)
     535            0 :             IF (.NOT. exist) THEN
     536              :                CALL cp_warn(__LOCATION__, &
     537              :                             "User requested to read the Hamiltonian matrix from the file named: "// &
     538            0 :                             TRIM(filename_h00_2)//". This file does not exist. The file will be created.")
     539            0 :                exist_all = .FALSE.
     540              :             END IF
     541            0 :             CALL negf_restart_file_name(filename_h01_2, exist, negf_section, logger, icontact, ispin=2, h01=.TRUE.)
     542            0 :             IF (.NOT. exist) THEN
     543              :                CALL cp_warn(__LOCATION__, &
     544              :                             "User requested to read the Hamiltonian matrix from the file named: "// &
     545            0 :                             TRIM(filename_h01_2)//". This file does not exist. The file will be created.")
     546            0 :                exist_all = .FALSE.
     547              :             END IF
     548              :          END IF
     549              :       END IF
     550            0 :       CALL para_env%bcast(exist_all)
     551              : 
     552            0 :       IF (exist_all) THEN
     553              : 
     554            0 :          negf_control%contacts(icontact)%is_restart = .TRUE.
     555            0 :          IF (log_unit > 0) THEN
     556            0 :             WRITE (log_unit, '(/,T2,A)') "User requested to read the Hamiltonian and overlap matrices from files."
     557            0 :             WRITE (log_unit, '(T2,A)') "All restart files exist."
     558              :          END IF
     559              : 
     560              :          ! ++ create matrices: s_00, s_01, h_00, h_01
     561            0 :          IF (para_env%is_source()) THEN
     562              :             CALL open_file(file_name=filename_s00, file_status="OLD", &
     563              :                            file_form="FORMATTED", file_action="READ", &
     564            0 :                            file_position="REWIND", unit_number=print_unit)
     565            0 :             READ (print_unit, *) nrow, ncol
     566            0 :             CALL close_file(print_unit)
     567              :          END IF
     568            0 :          CALL para_env%bcast(nrow)
     569            0 :          CALL para_env%bcast(ncol)
     570            0 :          NULLIFY (fm_struct)
     571            0 :          CALL cp_fm_struct_create(fm_struct, nrow_global=nrow, ncol_global=ncol, context=sub_env%blacs_env)
     572            0 :          ALLOCATE (negf_env%contacts(icontact)%s_00, negf_env%contacts(icontact)%s_01)
     573            0 :          CALL cp_fm_create(negf_env%contacts(icontact)%s_00, fm_struct)
     574            0 :          CALL cp_fm_create(negf_env%contacts(icontact)%s_01, fm_struct)
     575            0 :          ALLOCATE (negf_env%contacts(icontact)%h_00(nspins), negf_env%contacts(icontact)%h_01(nspins))
     576            0 :          DO ispin = 1, nspins
     577            0 :             CALL cp_fm_create(negf_env%contacts(icontact)%h_00(ispin), fm_struct)
     578            0 :             CALL cp_fm_create(negf_env%contacts(icontact)%h_01(ispin), fm_struct)
     579              :          END DO
     580            0 :          CALL cp_fm_struct_release(fm_struct)
     581              : 
     582            0 :          ALLOCATE (target_m(nrow, ncol))
     583            0 :          IF (para_env%is_source()) CALL negf_read_matrix_from_file(filename_s00, target_m)
     584            0 :          CALL para_env%bcast(target_m)
     585            0 :          CALL cp_fm_set_submatrix(negf_env%contacts(icontact)%s_00, target_m)
     586            0 :          IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "S_00 is read from "//TRIM(filename_s00)
     587            0 :          IF (para_env%is_source()) CALL negf_read_matrix_from_file(filename_s01, target_m)
     588            0 :          CALL para_env%bcast(target_m)
     589            0 :          CALL cp_fm_set_submatrix(negf_env%contacts(icontact)%s_01, target_m)
     590            0 :          IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "S_01 is read from "//TRIM(filename_s01)
     591            0 :          IF (nspins == 1) THEN
     592            0 :             IF (para_env%is_source()) CALL negf_read_matrix_from_file(filename_h00_1, target_m)
     593            0 :             CALL para_env%bcast(target_m)
     594            0 :             CALL cp_fm_set_submatrix(negf_env%contacts(icontact)%h_00(1), target_m)
     595            0 :             IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_00 is read from "//TRIM(filename_h00_1)
     596            0 :             IF (para_env%is_source()) CALL negf_read_matrix_from_file(filename_h01_1, target_m)
     597            0 :             CALL para_env%bcast(target_m)
     598            0 :             CALL cp_fm_set_submatrix(negf_env%contacts(icontact)%h_01(1), target_m)
     599            0 :             IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_01 is read from "//TRIM(filename_H01_1)
     600              :          END IF
     601            0 :          IF (nspins == 2) THEN
     602            0 :             IF (para_env%is_source()) CALL negf_read_matrix_from_file(filename_h00_1, target_m)
     603            0 :             CALL para_env%bcast(target_m)
     604            0 :             CALL cp_fm_set_submatrix(negf_env%contacts(icontact)%h_00(1), target_m)
     605            0 :             IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_00 is read from "//TRIM(filename_h00_1)//" for spin 1"
     606            0 :             IF (para_env%is_source()) CALL negf_read_matrix_from_file(filename_h01_1, target_m)
     607            0 :             CALL para_env%bcast(target_m)
     608            0 :             CALL cp_fm_set_submatrix(negf_env%contacts(icontact)%h_01(1), target_m)
     609            0 :             IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_01 is read from "//TRIM(filename_H01_1)//" for spin 1"
     610            0 :             IF (para_env%is_source()) CALL negf_read_matrix_from_file(filename_h00_2, target_m)
     611            0 :             CALL para_env%bcast(target_m)
     612            0 :             CALL cp_fm_set_submatrix(negf_env%contacts(icontact)%h_00(2), target_m)
     613            0 :             IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_00 is read from "//TRIM(filename_h00_2)//" for spin 2"
     614            0 :             IF (para_env%is_source()) CALL negf_read_matrix_from_file(filename_h01_2, target_m)
     615            0 :             CALL para_env%bcast(target_m)
     616            0 :             CALL cp_fm_set_submatrix(negf_env%contacts(icontact)%h_01(2), target_m)
     617            0 :             IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_01 is read from "//TRIM(filename_H01_2)//" for spin 2"
     618              :          END IF
     619            0 :          DEALLOCATE (target_m)
     620              : 
     621              :       ELSE
     622              : 
     623            0 :          IF (log_unit > 0) WRITE (log_unit, '(T2,A)') &
     624            0 :             "Some restart files do not exist. ALL restart files will be recalculated!"
     625              : 
     626            0 :          IF (is_separate) THEN
     627            0 :             IF (log_unit > 0) WRITE (log_unit, '(/,T2,A,T70,I11,/,A)') &
     628            0 :                "Construct the Kohn-Sham matrix from from the separate bulk DFT calculation"
     629            0 :             CALL qs_energies(qs_env_contact, consistent_energies=.FALSE., calc_forces=.FALSE.)
     630              :             CALL negf_env_contact_init_matrices(contact_env=negf_env%contacts(icontact), sub_env=sub_env, &
     631            0 :                                                 qs_env_contact=qs_env_contact)
     632              :          ELSE
     633            0 :             IF (log_unit > 0) WRITE (log_unit, '(/,T2,A,T70,I11,/,A)') &
     634            0 :                "Construct the Kohn-Sham matrix from the entire system bulk DFT calculation"
     635            0 :             negf_control%contacts(icontact)%read_write_HS = .FALSE.
     636            0 :             IF (.NOT. is_dft_entire) CALL qs_energies(qs_env_contact, consistent_energies=.FALSE., calc_forces=.FALSE.)
     637              :             CALL negf_env_contact_init_matrices_gamma(contact_env=negf_env%contacts(icontact), &
     638              :                                                       contact_control=negf_control%contacts(icontact), &
     639              :                                                       sub_env=sub_env, qs_env=qs_env_contact, &
     640            0 :                                                       eps_geometry=negf_control%eps_geometry)
     641            0 :             negf_control%contacts(icontact)%read_write_HS = .TRUE.
     642            0 :             is_dft_entire = .TRUE.
     643              :          END IF
     644              : 
     645            0 :          CALL cp_fm_get_info(negf_env%contacts(icontact)%s_00, nrow_global=nrow)
     646            0 :          ALLOCATE (target_m(nrow, nrow))
     647            0 :          CALL cp_fm_get_submatrix(negf_env%contacts(icontact)%s_00, target_m)
     648            0 :          IF (para_env%is_source()) CALL negf_print_matrix_to_file(filename_s00, target_m)
     649            0 :          IF (log_unit > 0) WRITE (log_unit, '(/,T2,A)') "S_00 is saved to "//TRIM(filename_s00)
     650            0 :          CALL cp_fm_get_submatrix(negf_env%contacts(icontact)%s_01, target_m)
     651            0 :          IF (para_env%is_source()) CALL negf_print_matrix_to_file(filename_s01, target_m)
     652            0 :          IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "S_01 is saved to "//TRIM(filename_s01)
     653            0 :          IF (nspins == 1) THEN
     654            0 :             CALL cp_fm_get_submatrix(negf_env%contacts(icontact)%h_00(1), target_m)
     655            0 :             IF (para_env%is_source()) CALL negf_print_matrix_to_file(filename_h00_1, target_m)
     656            0 :             IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_00 is saved to "//TRIM(filename_h00_1)
     657            0 :             CALL cp_fm_get_submatrix(negf_env%contacts(icontact)%h_01(1), target_m)
     658            0 :             IF (para_env%is_source()) CALL negf_print_matrix_to_file(filename_h01_1, target_m)
     659            0 :             IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_01 is saved to "//TRIM(filename_h01_1)
     660              :          END IF
     661            0 :          IF (nspins == 2) THEN
     662            0 :             CALL cp_fm_get_submatrix(negf_env%contacts(icontact)%h_00(1), target_m)
     663            0 :             IF (para_env%is_source()) CALL negf_print_matrix_to_file(filename_h00_1, target_m)
     664            0 :             IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_00 is saved to "//TRIM(filename_h00_1)//" for spin 1"
     665            0 :             CALL cp_fm_get_submatrix(negf_env%contacts(icontact)%h_01(1), target_m)
     666            0 :             IF (para_env%is_source()) CALL negf_print_matrix_to_file(filename_h01_1, target_m)
     667            0 :             IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_01 is saved to "//TRIM(filename_h01_1)//" for spin 1"
     668            0 :             CALL cp_fm_get_submatrix(negf_env%contacts(icontact)%h_00(2), target_m)
     669            0 :             IF (para_env%is_source()) CALL negf_print_matrix_to_file(filename_h00_2, target_m)
     670            0 :             IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_00 is saved to "//TRIM(filename_h00_2)//" for spin 2"
     671            0 :             CALL cp_fm_get_submatrix(negf_env%contacts(icontact)%h_01(2), target_m)
     672            0 :             IF (para_env%is_source()) CALL negf_print_matrix_to_file(filename_h01_2, target_m)
     673            0 :             IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_01 is saved to "//TRIM(filename_h01_2)//" for spin 2"
     674              :          END IF
     675            0 :          DEALLOCATE (target_m)
     676              : 
     677            0 :          negf_control%write_common_restart_file = .TRUE.
     678              : 
     679              :       END IF
     680              : 
     681            0 :       IF (log_unit > 0) WRITE (log_unit, '(/,T2,79("-"))')
     682              : 
     683            0 :       CALL timestop(handle)
     684            0 :    END SUBROUTINE negf_env_contact_read_write_hs
     685              : 
     686              : ! **************************************************************************************************
     687              : !> \brief Extract relevant matrix blocks for the given contact.
     688              : !> \param contact_env         NEGF environment for the contact (modified on exit)
     689              : !> \param sub_env             NEGF parallel (sub)group environment
     690              : !> \param qs_env_contact      QuickStep environment for the contact force environment
     691              : !> \par History
     692              : !>   * 10.2017 created [Sergey Chulkov]
     693              : !>   * 10.2025 The subroutine is essentially modified. New functionality of negf_copy_contact_matrix.
     694              : !>             [Dmitry Ryndyk]
     695              : ! **************************************************************************************************
     696            4 :    SUBROUTINE negf_env_contact_init_matrices(contact_env, sub_env, qs_env_contact)
     697              :       TYPE(negf_env_contact_type), INTENT(inout)         :: contact_env
     698              :       TYPE(negf_subgroup_env_type), INTENT(in)           :: sub_env
     699              :       TYPE(qs_environment_type), POINTER                 :: qs_env_contact
     700              : 
     701              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'negf_env_contact_init_matrices'
     702              : 
     703              :       INTEGER                                            :: handle, iatom, ispin, nao, natoms, &
     704              :                                                             nimages, nspins
     705            4 :       INTEGER, ALLOCATABLE, DIMENSION(:)                 :: atom_list0, atom_list1
     706            4 :       INTEGER, ALLOCATABLE, DIMENSION(:, :)              :: index_to_cell
     707            4 :       INTEGER, DIMENSION(:, :, :), POINTER               :: cell_to_index
     708              :       LOGICAL                                            :: do_kpoints
     709              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct
     710            4 :       TYPE(dbcsr_p_type), DIMENSION(:), POINTER          :: matkp
     711            4 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_ks_kp, matrix_s_kp, rho_ao_kp
     712              :       TYPE(dft_control_type), POINTER                    :: dft_control
     713              :       TYPE(kpoint_type), POINTER                         :: kpoints
     714              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     715              :       TYPE(qs_rho_type), POINTER                         :: rho_struct
     716              :       TYPE(qs_subsys_type), POINTER                      :: subsys
     717              : 
     718            4 :       CALL timeset(routineN, handle)
     719              : 
     720              :       CALL get_qs_env(qs_env_contact, &
     721              :                       dft_control=dft_control, &
     722              :                       do_kpoints=do_kpoints, &
     723              :                       kpoints=kpoints, &
     724              :                       matrix_ks_kp=matrix_ks_kp, &
     725              :                       matrix_s_kp=matrix_s_kp, &
     726              :                       para_env=para_env, &
     727              :                       rho=rho_struct, &
     728            4 :                       subsys=subsys)
     729            4 :       CALL qs_rho_get(rho_struct, rho_ao_kp=rho_ao_kp)
     730              : 
     731            4 :       CALL negf_homo_energy_estimate(contact_env%homo_energy, qs_env_contact)
     732              : 
     733            4 :       natoms = SIZE(contact_env%atomlist_cell0)
     734           12 :       ALLOCATE (atom_list0(natoms))
     735           20 :       DO iatom = 1, natoms
     736           16 :          atom_list0(iatom) = contact_env%atom_map_cell0(iatom)%iatom
     737              : 
     738              :          ! with no k-points there is one-to-one correspondence between the primary unit cell
     739              :          ! of the contact force_env and the first contact unit cell of the device force_env
     740           68 :          IF (SUM(ABS(contact_env%atom_map_cell0(iatom)%cell(:))) > 0) THEN
     741            0 :             CPABORT("NEGF K-points are not currently supported")
     742              :          END IF
     743              :       END DO
     744              : 
     745            4 :       CPASSERT(SIZE(contact_env%atomlist_cell1) == natoms)
     746            8 :       ALLOCATE (atom_list1(natoms))
     747           20 :       DO iatom = 1, natoms
     748           20 :          atom_list1(iatom) = contact_env%atom_map_cell1(iatom)%iatom
     749              :       END DO
     750              : 
     751            4 :       nspins = dft_control%nspins
     752            4 :       nimages = dft_control%nimages
     753              : 
     754            4 :       IF (do_kpoints) THEN
     755            4 :          CALL get_kpoint_info(kpoints, cell_to_index=cell_to_index)
     756              :       ELSE
     757            0 :          ALLOCATE (cell_to_index(0:0, 0:0, 0:0))
     758            0 :          cell_to_index(0, 0, 0) = 1
     759              :       END IF
     760              : 
     761           12 :       ALLOCATE (index_to_cell(3, nimages))
     762            4 :       CALL invert_cell_to_index(cell_to_index, nimages, index_to_cell)
     763            4 :       IF (.NOT. do_kpoints) DEALLOCATE (cell_to_index)
     764              : 
     765            4 :       NULLIFY (fm_struct)
     766            4 :       nao = number_of_atomic_orbitals(subsys, atom_list0)
     767            4 :       CALL cp_fm_struct_create(fm_struct, nrow_global=nao, ncol_global=nao, context=sub_env%blacs_env)
     768              : 
     769              :       ! ++ create matrices: s_00, s_01
     770            4 :       ALLOCATE (contact_env%s_00, contact_env%s_01)
     771            4 :       CALL cp_fm_create(contact_env%s_00, fm_struct)
     772            4 :       CALL cp_fm_create(contact_env%s_01, fm_struct)
     773              : 
     774              :       ! ++ create matrices: h_00, h_01, rho_00, rho_01
     775           24 :       ALLOCATE (contact_env%h_00(nspins), contact_env%h_01(nspins))
     776           20 :       ALLOCATE (contact_env%rho_00(nspins), contact_env%rho_01(nspins))
     777            8 :       DO ispin = 1, nspins
     778            4 :          CALL cp_fm_create(contact_env%h_00(ispin), fm_struct)
     779            4 :          CALL cp_fm_create(contact_env%h_01(ispin), fm_struct)
     780            4 :          CALL cp_fm_create(contact_env%rho_00(ispin), fm_struct)
     781            8 :          CALL cp_fm_create(contact_env%rho_01(ispin), fm_struct)
     782              :       END DO
     783              : 
     784            4 :       CALL cp_fm_struct_release(fm_struct)
     785              : 
     786              :       ! extract matrices: s_00, s_01
     787            4 :       matkp => matrix_s_kp(1, :)
     788              :       CALL negf_copy_contact_matrix(fm_cell0=contact_env%s_00, &
     789              :                                     fm_cell1=contact_env%s_01, &
     790              :                                     direction_axis=contact_env%direction_axis, &
     791              :                                     matrix_kp=matkp, &
     792              :                                     atom_list0=atom_list0, atom_list1=atom_list1, &
     793              :                                     subsys=subsys, mpi_comm_global=para_env, &
     794            4 :                                     kpoints=kpoints)
     795              : 
     796              :       ! extract matrices: h_00, h_01, rho_00, rho_01
     797            8 :       DO ispin = 1, nspins
     798            4 :          matkp => matrix_ks_kp(ispin, :)
     799              :          CALL negf_copy_contact_matrix(fm_cell0=contact_env%h_00(ispin), &
     800              :                                        fm_cell1=contact_env%h_01(ispin), &
     801              :                                        direction_axis=contact_env%direction_axis, &
     802              :                                        matrix_kp=matkp, &
     803              :                                        atom_list0=atom_list0, atom_list1=atom_list1, &
     804              :                                        subsys=subsys, mpi_comm_global=para_env, &
     805            4 :                                        kpoints=kpoints)
     806              : 
     807            4 :          matkp => rho_ao_kp(ispin, :)
     808              :          CALL negf_copy_contact_matrix(fm_cell0=contact_env%rho_00(ispin), &
     809              :                                        fm_cell1=contact_env%rho_01(ispin), &
     810              :                                        direction_axis=contact_env%direction_axis, &
     811              :                                        matrix_kp=matkp, &
     812              :                                        atom_list0=atom_list0, atom_list1=atom_list1, &
     813              :                                        subsys=subsys, mpi_comm_global=para_env, &
     814            8 :                                        kpoints=kpoints)
     815              :       END DO
     816              : 
     817            4 :       DEALLOCATE (atom_list0, atom_list1)
     818              : 
     819            4 :       CALL timestop(handle)
     820            8 :    END SUBROUTINE negf_env_contact_init_matrices
     821              : 
     822              : ! **************************************************************************************************
     823              : !> \brief Extract relevant matrix blocks for the given contact using the device's force environment.
     824              : !> \param contact_env         NEGF environment for the contact (modified on exit)
     825              : !> \param contact_control     NEGF control for the contact
     826              : !> \param sub_env             NEGF parallel (sub)group environment
     827              : !> \param qs_env              QuickStep environment for the device force environment
     828              : !> \param eps_geometry        accuracy in Cartesian coordinates
     829              : !> \author Sergey Chulkov
     830              : ! **************************************************************************************************
     831            4 :    SUBROUTINE negf_env_contact_init_matrices_gamma(contact_env, contact_control, sub_env, qs_env, eps_geometry)
     832              :       TYPE(negf_env_contact_type), INTENT(inout)         :: contact_env
     833              :       TYPE(negf_control_contact_type), INTENT(in)        :: contact_control
     834              :       TYPE(negf_subgroup_env_type), INTENT(in)           :: sub_env
     835              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     836              :       REAL(kind=dp), INTENT(in)                          :: eps_geometry
     837              : 
     838              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'negf_env_contact_init_matrices_gamma'
     839              : 
     840              :       INTEGER                                            :: handle, iatom, icell, ispin, nao_c, &
     841              :                                                             nspins
     842              :       LOGICAL                                            :: do_kpoints
     843              :       REAL(kind=dp), DIMENSION(2)                        :: r2_origin_cell
     844              :       REAL(kind=dp), DIMENSION(3)                        :: direction_vector, origin
     845              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct
     846            4 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_ks_kp, matrix_s_kp, rho_ao_kp
     847              :       TYPE(dft_control_type), POINTER                    :: dft_control
     848              :       TYPE(mp_para_env_type), POINTER                    :: para_env
     849            4 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     850              :       TYPE(qs_rho_type), POINTER                         :: rho_struct
     851              :       TYPE(qs_subsys_type), POINTER                      :: subsys
     852              : 
     853            4 :       CALL timeset(routineN, handle)
     854              : 
     855              :       CALL get_qs_env(qs_env, &
     856              :                       dft_control=dft_control, &
     857              :                       do_kpoints=do_kpoints, &
     858              :                       matrix_ks_kp=matrix_ks_kp, &
     859              :                       matrix_s_kp=matrix_s_kp, &
     860              :                       para_env=para_env, &
     861              :                       rho=rho_struct, &
     862            4 :                       subsys=subsys)
     863            4 :       CALL qs_rho_get(rho_struct, rho_ao_kp=rho_ao_kp)
     864              : 
     865            4 :       IF (do_kpoints) THEN
     866              :          CALL cp_abort(__LOCATION__, &
     867            0 :                        "K-points in device region have not been implemented yet.")
     868              :       END IF
     869              : 
     870            4 :       nspins = dft_control%nspins
     871              : 
     872            4 :       nao_c = number_of_atomic_orbitals(subsys, contact_control%atomlist_cell(1)%vector)
     873            4 :       IF (number_of_atomic_orbitals(subsys, contact_control%atomlist_cell(2)%vector) /= nao_c) THEN
     874              :          CALL cp_abort(__LOCATION__, &
     875              :                        "Primary and secondary bulk contact cells should be identical "// &
     876              :                        "in terms of the number of atoms of each kind, and their basis sets. "// &
     877            0 :                        "No single atom, however, can be shared between these two cells.")
     878              :       END IF
     879              : 
     880            4 :       contact_env%homo_energy = 0.0_dp
     881              : 
     882              :       CALL contact_direction_vector(contact_env%origin, &
     883              :                                     contact_env%direction_vector, &
     884              :                                     contact_env%origin_bias, &
     885              :                                     contact_env%direction_vector_bias, &
     886              :                                     contact_control%atomlist_screening, &
     887              :                                     contact_control%atomlist_bulk, &
     888            4 :                                     subsys)
     889              : 
     890            4 :       contact_env%direction_axis = contact_direction_axis(contact_env%direction_vector, subsys, eps_geometry)
     891              : 
     892              :       ! choose the primary and secondary contact unit cells
     893            4 :       CALL qs_subsys_get(subsys, particle_set=particle_set)
     894              : 
     895           16 :       origin = particle_set(contact_control%atomlist_screening(1))%r
     896           16 :       DO iatom = 2, SIZE(contact_control%atomlist_screening)
     897           52 :          origin = origin + particle_set(contact_control%atomlist_screening(iatom))%r
     898              :       END DO
     899           16 :       origin = origin/REAL(SIZE(contact_control%atomlist_screening), kind=dp)
     900              : 
     901           12 :       DO icell = 1, 2
     902           32 :          direction_vector = particle_set(contact_control%atomlist_cell(icell)%vector(1))%r
     903           32 :          DO iatom = 2, SIZE(contact_control%atomlist_cell(icell)%vector)
     904          104 :             direction_vector = direction_vector + particle_set(contact_control%atomlist_cell(icell)%vector(iatom))%r
     905              :          END DO
     906           32 :          direction_vector = direction_vector/REAL(SIZE(contact_control%atomlist_cell(icell)%vector), kind=dp)
     907           32 :          direction_vector = direction_vector - origin
     908           36 :          r2_origin_cell(icell) = DOT_PRODUCT(direction_vector, direction_vector)
     909              :       END DO
     910              : 
     911            4 :       IF (ABS(r2_origin_cell(1) - r2_origin_cell(2)) < (eps_geometry*eps_geometry)) THEN
     912              :          ! primary and secondary bulk unit cells should not overlap;
     913              :          ! currently we check that they are different by at least one atom that is, indeed, not sufficient.
     914              :          CALL cp_abort(__LOCATION__, &
     915            0 :                        "Primary and secondary bulk contact cells should not overlap ")
     916            4 :       ELSE IF (r2_origin_cell(1) < r2_origin_cell(2)) THEN
     917            2 :          IF (.NOT. ALLOCATED(contact_env%atomlist_cell0)) &
     918            6 :             ALLOCATE (contact_env%atomlist_cell0(SIZE(contact_control%atomlist_cell(1)%vector)))
     919           10 :          contact_env%atomlist_cell0(:) = contact_control%atomlist_cell(1)%vector(:)
     920            2 :          IF (.NOT. ALLOCATED(contact_env%atomlist_cell1)) &
     921            6 :             ALLOCATE (contact_env%atomlist_cell1(SIZE(contact_control%atomlist_cell(2)%vector)))
     922           10 :          contact_env%atomlist_cell1(:) = contact_control%atomlist_cell(2)%vector(:)
     923              :       ELSE
     924            2 :          IF (.NOT. ALLOCATED(contact_env%atomlist_cell0)) &
     925            6 :             ALLOCATE (contact_env%atomlist_cell0(SIZE(contact_control%atomlist_cell(2)%vector)))
     926           10 :          contact_env%atomlist_cell0(:) = contact_control%atomlist_cell(2)%vector(:)
     927            2 :          IF (.NOT. ALLOCATED(contact_env%atomlist_cell1)) &
     928            6 :             ALLOCATE (contact_env%atomlist_cell1(SIZE(contact_control%atomlist_cell(1)%vector)))
     929           10 :          contact_env%atomlist_cell1(:) = contact_control%atomlist_cell(1)%vector(:)
     930              :       END IF
     931            4 :       IF (.NOT. contact_control%read_write_HS) THEN
     932            4 :          NULLIFY (fm_struct)
     933            4 :          CALL cp_fm_struct_create(fm_struct, nrow_global=nao_c, ncol_global=nao_c, context=sub_env%blacs_env)
     934           28 :          ALLOCATE (contact_env%h_00(nspins), contact_env%h_01(nspins))
     935           28 :          ALLOCATE (contact_env%rho_00(nspins), contact_env%rho_01(nspins))
     936            8 :          DO ispin = 1, nspins
     937            4 :             CALL cp_fm_create(contact_env%h_00(ispin), fm_struct)
     938            4 :             CALL cp_fm_create(contact_env%h_01(ispin), fm_struct)
     939            4 :             CALL cp_fm_create(contact_env%rho_00(ispin), fm_struct)
     940            8 :             CALL cp_fm_create(contact_env%rho_01(ispin), fm_struct)
     941              :          END DO
     942            4 :          ALLOCATE (contact_env%s_00, contact_env%s_01)
     943            4 :          CALL cp_fm_create(contact_env%s_00, fm_struct)
     944            4 :          CALL cp_fm_create(contact_env%s_01, fm_struct)
     945            4 :          CALL cp_fm_struct_release(fm_struct)
     946              : 
     947            8 :          DO ispin = 1, nspins
     948              :             CALL negf_copy_sym_dbcsr_to_fm_submat(matrix=matrix_ks_kp(ispin, 1)%matrix, &
     949              :                                                   fm=contact_env%h_00(ispin), &
     950              :                                                   atomlist_row=contact_env%atomlist_cell0, &
     951              :                                                   atomlist_col=contact_env%atomlist_cell0, &
     952              :                                                   subsys=subsys, mpi_comm_global=para_env, &
     953            4 :                                                   do_upper_diag=.TRUE., do_lower=.TRUE.)
     954              :             CALL negf_copy_sym_dbcsr_to_fm_submat(matrix=matrix_ks_kp(ispin, 1)%matrix, &
     955              :                                                   fm=contact_env%h_01(ispin), &
     956              :                                                   atomlist_row=contact_env%atomlist_cell0, &
     957              :                                                   atomlist_col=contact_env%atomlist_cell1, &
     958              :                                                   subsys=subsys, mpi_comm_global=para_env, &
     959            4 :                                                   do_upper_diag=.TRUE., do_lower=.TRUE.)
     960              : 
     961              :             CALL negf_copy_sym_dbcsr_to_fm_submat(matrix=rho_ao_kp(ispin, 1)%matrix, &
     962              :                                                   fm=contact_env%rho_00(ispin), &
     963              :                                                   atomlist_row=contact_env%atomlist_cell0, &
     964              :                                                   atomlist_col=contact_env%atomlist_cell0, &
     965              :                                                   subsys=subsys, mpi_comm_global=para_env, &
     966            4 :                                                   do_upper_diag=.TRUE., do_lower=.TRUE.)
     967              :             CALL negf_copy_sym_dbcsr_to_fm_submat(matrix=rho_ao_kp(ispin, 1)%matrix, &
     968              :                                                   fm=contact_env%rho_01(ispin), &
     969              :                                                   atomlist_row=contact_env%atomlist_cell0, &
     970              :                                                   atomlist_col=contact_env%atomlist_cell1, &
     971              :                                                   subsys=subsys, mpi_comm_global=para_env, &
     972            8 :                                                   do_upper_diag=.TRUE., do_lower=.TRUE.)
     973              :          END DO
     974              : 
     975              :          CALL negf_copy_sym_dbcsr_to_fm_submat(matrix=matrix_s_kp(1, 1)%matrix, &
     976              :                                                fm=contact_env%s_00, &
     977              :                                                atomlist_row=contact_env%atomlist_cell0, &
     978              :                                                atomlist_col=contact_env%atomlist_cell0, &
     979              :                                                subsys=subsys, mpi_comm_global=para_env, &
     980            4 :                                                do_upper_diag=.TRUE., do_lower=.TRUE.)
     981              :          CALL negf_copy_sym_dbcsr_to_fm_submat(matrix=matrix_s_kp(1, 1)%matrix, &
     982              :                                                fm=contact_env%s_01, &
     983              :                                                atomlist_row=contact_env%atomlist_cell0, &
     984              :                                                atomlist_col=contact_env%atomlist_cell1, &
     985              :                                                subsys=subsys, mpi_comm_global=para_env, &
     986            4 :                                                do_upper_diag=.TRUE., do_lower=.TRUE.)
     987              :       END IF
     988            4 :       CALL timestop(handle)
     989            4 :    END SUBROUTINE negf_env_contact_init_matrices_gamma
     990              : 
     991              : ! **************************************************************************************************
     992              : !> \brief Reading and writing of the electrode Hamiltonian and overlap matrices from/to a file.
     993              : !> \param force_env    ...
     994              : !> \param para_env     ...
     995              : !> \param negf_env     ...
     996              : !> \param sub_env      ...
     997              : !> \param negf_control ...
     998              : !> \param negf_section ...
     999              : !> \param log_unit     ...
    1000              : !> \param is_dft_entire ...
    1001              : !> \par History
    1002              : !>    * 01.2026 created  [Dmitry Ryndyk]
    1003              : ! **************************************************************************************************
    1004            0 :    SUBROUTINE negf_env_scatt_read_write_hs(force_env, para_env, negf_env, sub_env, negf_control, negf_section, &
    1005              :                                            log_unit, is_dft_entire)
    1006              :       TYPE(force_env_type), POINTER                      :: force_env
    1007              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1008              :       TYPE(negf_env_type), INTENT(inout)                 :: negf_env
    1009              :       TYPE(negf_subgroup_env_type), INTENT(in)           :: sub_env
    1010              :       TYPE(negf_control_type), POINTER                   :: negf_control
    1011              :       TYPE(section_vals_type), POINTER                   :: negf_section
    1012              :       INTEGER, INTENT(in)                                :: log_unit
    1013              :       LOGICAL, INTENT(inout), OPTIONAL                   :: is_dft_entire
    1014              : 
    1015              :       CHARACTER(len=*), PARAMETER :: routineN = 'negf_env_scatt_read_write_hs'
    1016              : 
    1017              :       CHARACTER(len=default_path_length)                 :: filename_h_1, filename_h_2, filename_s
    1018              :       CHARACTER(len=default_path_length), ALLOCATABLE, &
    1019            0 :          DIMENSION(:)                                    :: filename_hc_1, filename_hc_2, filename_sc
    1020              :       INTEGER                                            :: handle, icontact, ispin, ncol_s, &
    1021              :                                                             ncol_sc, ncontacts, nrow_s, nrow_sc, &
    1022              :                                                             nspins, print_unit
    1023              :       LOGICAL                                            :: exist, exist_all
    1024            0 :       REAL(KIND=dp), ALLOCATABLE, DIMENSION(:, :)        :: target_m
    1025              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct
    1026              :       TYPE(cp_logger_type), POINTER                      :: logger
    1027              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1028              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1029              :       TYPE(qs_subsys_type), POINTER                      :: subsys
    1030              : 
    1031            0 :       CALL timeset(routineN, handle)
    1032            0 :       logger => cp_get_default_logger()
    1033              : 
    1034            0 :       CALL force_env_get(force_env, qs_env=qs_env)
    1035            0 :       CALL get_qs_env(qs_env, dft_control=dft_control, subsys=subsys)
    1036            0 :       ncontacts = SIZE(negf_control%contacts)
    1037            0 :       nspins = dft_control%nspins
    1038            0 :       ALLOCATE (filename_sc(ncontacts), filename_hc_1(ncontacts), filename_hc_2(ncontacts))
    1039              : 
    1040              :       ! Check that the files exist.
    1041              :       ! ispin=0 is used to show nspins=1
    1042            0 :       exist_all = .TRUE.
    1043            0 :       IF (para_env%is_source()) THEN
    1044            0 :          CALL negf_restart_file_name(filename_s, exist, negf_section, logger, s=.TRUE.)
    1045            0 :          IF (.NOT. exist) THEN
    1046              :             CALL cp_warn(__LOCATION__, &
    1047              :                          "User requested to read the overlap matrix from the file named: "// &
    1048            0 :                          TRIM(filename_s)//". This file does not exist. The file will be created.")
    1049            0 :             exist_all = .FALSE.
    1050              :          END IF
    1051            0 :          IF (nspins == 1) THEN
    1052            0 :             CALL negf_restart_file_name(filename_h_1, exist, negf_section, logger, ispin=0, h=.TRUE.)
    1053            0 :             IF (.NOT. exist) THEN
    1054              :                CALL cp_warn(__LOCATION__, &
    1055              :                             "User requested to read the Hamiltonian matrix from the file named: "// &
    1056            0 :                             TRIM(filename_h_1)//". This file does not exist. The file will be created.")
    1057            0 :                exist_all = .FALSE.
    1058              :             END IF
    1059              :          END IF
    1060            0 :          IF (nspins == 2) THEN
    1061            0 :             CALL negf_restart_file_name(filename_h_1, exist, negf_section, logger, ispin=1, h=.TRUE.)
    1062            0 :             IF (.NOT. exist) THEN
    1063              :                CALL cp_warn(__LOCATION__, &
    1064              :                             "User requested to read the Hamiltonian matrix from the file named: "// &
    1065            0 :                             TRIM(filename_h_1)//". This file does not exist. The file will be created.")
    1066            0 :                exist_all = .FALSE.
    1067              :             END IF
    1068            0 :             CALL negf_restart_file_name(filename_h_2, exist, negf_section, logger, ispin=2, h=.TRUE.)
    1069            0 :             IF (.NOT. exist) THEN
    1070              :                CALL cp_warn(__LOCATION__, &
    1071              :                             "User requested to read the Hamiltonian matrix from the file named: "// &
    1072            0 :                             TRIM(filename_h_2)//". This file does not exist. The file will be created.")
    1073            0 :                exist_all = .FALSE.
    1074              :             END IF
    1075              :          END IF
    1076            0 :          DO icontact = 1, ncontacts
    1077            0 :             CALL negf_restart_file_name(filename_sc(icontact), exist, negf_section, logger, icontact=icontact, sc=.TRUE.)
    1078            0 :             IF (.NOT. exist) THEN
    1079              :                CALL cp_warn(__LOCATION__, &
    1080              :                             "User requested to read the overlap matrix from the file named: "// &
    1081            0 :                             TRIM(filename_sc(icontact))//". This file does not exist. The file will be created.")
    1082            0 :                exist_all = .FALSE.
    1083              :             END IF
    1084            0 :             IF (nspins == 1) THEN
    1085              :                CALL negf_restart_file_name(filename_hc_1(icontact), exist, negf_section, logger, icontact=icontact, &
    1086            0 :                                            ispin=0, hc=.TRUE.)
    1087            0 :                IF (.NOT. exist) THEN
    1088              :                   CALL cp_warn(__LOCATION__, &
    1089              :                                "User requested to read the Hamiltonian matrix from the file named: "// &
    1090            0 :                                TRIM(filename_hc_1(icontact))//". This file does not exist. The file will be created.")
    1091            0 :                   exist_all = .FALSE.
    1092              :                END IF
    1093              :             END IF
    1094            0 :             IF (nspins == 2) THEN
    1095              :                CALL negf_restart_file_name(filename_hc_1(icontact), exist, negf_section, logger, icontact=icontact, &
    1096            0 :                                            ispin=1, hc=.TRUE.)
    1097            0 :                IF (.NOT. exist) THEN
    1098              :                   CALL cp_warn(__LOCATION__, &
    1099              :                                "User requested to read the Hamiltonian matrix from the file named: "// &
    1100            0 :                                TRIM(filename_hc_1(icontact))//". This file does not exist. The file will be created.")
    1101            0 :                   exist_all = .FALSE.
    1102              :                END IF
    1103              :                CALL negf_restart_file_name(filename_hc_2(icontact), exist, negf_section, logger, icontact=icontact, &
    1104            0 :                                            ispin=2, hc=.TRUE.)
    1105            0 :                IF (.NOT. exist) THEN
    1106              :                   CALL cp_warn(__LOCATION__, &
    1107              :                                "User requested to read the Hamiltonian matrix from the file named: "// &
    1108            0 :                                TRIM(filename_hc_2(icontact))//". This file does not exist. The file will be created.")
    1109            0 :                   exist_all = .FALSE.
    1110              :                END IF
    1111              :             END IF
    1112              :          END DO
    1113              :       END IF
    1114            0 :       CALL para_env%bcast(exist_all)
    1115              : 
    1116            0 :       IF (exist_all) THEN
    1117              : 
    1118            0 :          negf_control%is_restart = .TRUE.
    1119              : 
    1120            0 :          IF (log_unit > 0) THEN
    1121            0 :             WRITE (log_unit, '(/,T2,A)') "User requested to read the Hamiltonian and overlap matrices from files."
    1122            0 :             WRITE (log_unit, '(T2,A)') "All restart files exist."
    1123              :          END IF
    1124              : 
    1125              :          ! ++ create matrices: s_s, s_sc, h_s, h_sc
    1126            0 :          IF (para_env%is_source()) THEN
    1127              :             CALL open_file(file_name=filename_s, file_status="OLD", &
    1128              :                            file_form="FORMATTED", file_action="READ", &
    1129            0 :                            file_position="REWIND", unit_number=print_unit)
    1130            0 :             READ (print_unit, *) nrow_s, ncol_s
    1131            0 :             CALL close_file(print_unit)
    1132              :          END IF
    1133            0 :          CALL para_env%bcast(nrow_s)
    1134            0 :          CALL para_env%bcast(ncol_s)
    1135            0 :          NULLIFY (fm_struct)
    1136            0 :          CALL cp_fm_struct_create(fm_struct, nrow_global=nrow_s, ncol_global=ncol_s, context=sub_env%blacs_env)
    1137            0 :          ALLOCATE (negf_env%s_s)
    1138            0 :          CALL cp_fm_create(negf_env%s_s, fm_struct)
    1139            0 :          ALLOCATE (negf_env%h_s(nspins))
    1140            0 :          DO ispin = 1, nspins
    1141            0 :             CALL cp_fm_create(negf_env%h_s(ispin), fm_struct)
    1142              :          END DO
    1143            0 :          CALL cp_fm_struct_release(fm_struct)
    1144            0 :          ALLOCATE (negf_env%s_sc(ncontacts))
    1145            0 :          ALLOCATE (negf_env%h_sc(nspins, ncontacts))
    1146            0 :          DO icontact = 1, ncontacts
    1147            0 :             IF (para_env%is_source()) THEN
    1148              :                CALL open_file(file_name=filename_sc(icontact), file_status="OLD", &
    1149              :                               file_form="FORMATTED", file_action="READ", &
    1150            0 :                               file_position="REWIND", unit_number=print_unit)
    1151            0 :                READ (print_unit, *) nrow_sc, ncol_sc
    1152            0 :                CALL close_file(print_unit)
    1153              :             END IF
    1154            0 :             CALL para_env%bcast(nrow_sc)
    1155            0 :             CALL para_env%bcast(ncol_sc)
    1156            0 :             NULLIFY (fm_struct)
    1157            0 :             CALL cp_fm_struct_create(fm_struct, nrow_global=nrow_sc, ncol_global=ncol_sc, context=sub_env%blacs_env)
    1158            0 :             CALL cp_fm_create(negf_env%s_sc(icontact), fm_struct)
    1159            0 :             DO ispin = 1, nspins
    1160            0 :                CALL cp_fm_create(negf_env%h_sc(ispin, icontact), fm_struct)
    1161              :             END DO
    1162            0 :             CALL cp_fm_struct_release(fm_struct)
    1163              :          END DO
    1164              : 
    1165            0 :          ALLOCATE (target_m(nrow_s, ncol_s))
    1166            0 :          IF (para_env%is_source()) CALL negf_read_matrix_from_file(filename_s, target_m)
    1167            0 :          CALL para_env%bcast(target_m)
    1168            0 :          CALL cp_fm_set_submatrix(negf_env%s_s, target_m)
    1169            0 :          IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "S_s is read from "//TRIM(filename_s)
    1170            0 :          IF (nspins == 1) THEN
    1171            0 :             IF (para_env%is_source()) CALL negf_read_matrix_from_file(filename_h_1, target_m)
    1172            0 :             CALL para_env%bcast(target_m)
    1173            0 :             CALL cp_fm_set_submatrix(negf_env%h_s(1), target_m)
    1174            0 :             IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_s is read from "//TRIM(filename_h_1)
    1175              :          END IF
    1176            0 :          IF (nspins == 2) THEN
    1177            0 :             IF (para_env%is_source()) CALL negf_read_matrix_from_file(filename_h_1, target_m)
    1178            0 :             CALL para_env%bcast(target_m)
    1179            0 :             CALL cp_fm_set_submatrix(negf_env%h_s(1), target_m)
    1180            0 :             IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_s is read from "//TRIM(filename_h_1)//" for spin 1"
    1181            0 :             IF (para_env%is_source()) CALL negf_read_matrix_from_file(filename_h_2, target_m)
    1182            0 :             CALL para_env%bcast(target_m)
    1183            0 :             CALL cp_fm_set_submatrix(negf_env%h_s(2), target_m)
    1184            0 :             IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_s is read from "//TRIM(filename_h_2)//" for spin 2"
    1185              :          END IF
    1186            0 :          DEALLOCATE (target_m)
    1187              : 
    1188            0 :          DO icontact = 1, ncontacts
    1189            0 :             ALLOCATE (target_m(nrow_s, ncol_sc))
    1190            0 :             IF (para_env%is_source()) CALL negf_read_matrix_from_file(filename_sc(icontact), target_m)
    1191            0 :             CALL para_env%bcast(target_m)
    1192            0 :             CALL cp_fm_set_submatrix(negf_env%s_sc(icontact), target_m)
    1193            0 :             IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "S_sc is read from "//TRIM(filename_sc(icontact))
    1194            0 :             IF (nspins == 1) THEN
    1195            0 :                IF (para_env%is_source()) CALL negf_read_matrix_from_file(filename_hc_1(icontact), target_m)
    1196            0 :                CALL para_env%bcast(target_m)
    1197            0 :                CALL cp_fm_set_submatrix(negf_env%h_sc(1, icontact), target_m)
    1198            0 :                IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_sc is read from "//TRIM(filename_hc_1(icontact))
    1199              :             END IF
    1200            0 :             IF (nspins == 2) THEN
    1201            0 :                IF (para_env%is_source()) CALL negf_read_matrix_from_file(filename_hc_1(icontact), target_m)
    1202            0 :                CALL para_env%bcast(target_m)
    1203            0 :                CALL cp_fm_set_submatrix(negf_env%h_sc(1, icontact), target_m)
    1204            0 :                IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_sc is read from "//TRIM(filename_hc_1(icontact))//" for spin 1"
    1205            0 :                IF (para_env%is_source()) CALL negf_read_matrix_from_file(filename_hc_2(icontact), target_m)
    1206            0 :                CALL para_env%bcast(target_m)
    1207            0 :                CALL cp_fm_set_submatrix(negf_env%h_sc(2, icontact), target_m)
    1208            0 :                IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_sc is read from "//TRIM(filename_hc_2(icontact))//" for spin 2"
    1209              :             END IF
    1210            0 :             DEALLOCATE (target_m)
    1211              : 
    1212              :          END DO
    1213              : 
    1214              :       ELSE
    1215              : 
    1216            0 :          IF (log_unit > 0) WRITE (log_unit, '(T2,A)') &
    1217            0 :             "Some restart files do not exist. ALL restart files will be recalculated!"
    1218              : 
    1219            0 :          IF (.NOT. is_dft_entire) CALL qs_energies(qs_env, consistent_energies=.FALSE., calc_forces=.FALSE.)
    1220              :          ! extract device-related matrix blocks
    1221            0 :          CALL negf_env_device_init_matrices(negf_env, negf_control, sub_env, qs_env)
    1222            0 :          is_dft_entire = .TRUE.
    1223              : 
    1224            0 :          CALL cp_fm_get_info(negf_env%s_s, nrow_global=nrow_s, ncol_global=ncol_s)
    1225            0 :          ALLOCATE (target_m(nrow_s, ncol_s))
    1226            0 :          CALL cp_fm_get_submatrix(negf_env%s_s, target_m)
    1227            0 :          IF (para_env%is_source()) CALL negf_print_matrix_to_file(filename_s, target_m)
    1228            0 :          IF (log_unit > 0) WRITE (log_unit, '(/,T2,A)') "S_s is saved to "//TRIM(filename_s)
    1229            0 :          IF (nspins == 1) THEN
    1230            0 :             CALL cp_fm_get_submatrix(negf_env%h_s(1), target_m)
    1231            0 :             IF (para_env%is_source()) CALL negf_print_matrix_to_file(filename_h_1, target_m)
    1232            0 :             IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_s is saved to "//TRIM(filename_h_1)
    1233              :          END IF
    1234            0 :          IF (nspins == 2) THEN
    1235            0 :             CALL cp_fm_get_submatrix(negf_env%h_s(1), target_m)
    1236            0 :             IF (para_env%is_source()) CALL negf_print_matrix_to_file(filename_h_1, target_m)
    1237            0 :             IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_s is saved to "//TRIM(filename_h_1)//" for spin 1"
    1238            0 :             CALL cp_fm_get_submatrix(negf_env%h_s(2), target_m)
    1239            0 :             IF (para_env%is_source()) CALL negf_print_matrix_to_file(filename_h_2, target_m)
    1240            0 :             IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_s is saved to "//TRIM(filename_h_2)//" for spin 2"
    1241              :          END IF
    1242            0 :          DEALLOCATE (target_m)
    1243              : 
    1244            0 :          DO icontact = 1, ncontacts
    1245            0 :             CALL cp_fm_get_info(negf_env%contacts(icontact)%s_00, nrow_global=nrow_sc, ncol_global=ncol_sc)
    1246            0 :             ALLOCATE (target_m(nrow_s, ncol_sc))
    1247            0 :             CALL cp_fm_get_submatrix(negf_env%s_sc(icontact), target_m)
    1248            0 :             IF (para_env%is_source()) CALL negf_print_matrix_to_file(filename_sc(icontact), target_m)
    1249            0 :             IF (log_unit > 0) WRITE (log_unit, '(T2,A,I3)') &
    1250            0 :                "S_sc is saved to "//TRIM(filename_sc(icontact))//" for contact", icontact
    1251            0 :             IF (nspins == 1) THEN
    1252            0 :                CALL cp_fm_get_submatrix(negf_env%h_sc(1, icontact), target_m)
    1253            0 :                IF (para_env%is_source()) CALL negf_print_matrix_to_file(filename_hc_1(icontact), target_m)
    1254            0 :                IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_sc is saved to "//TRIM(filename_hc_1(icontact))
    1255              :             END IF
    1256            0 :             IF (nspins == 2) THEN
    1257            0 :                CALL cp_fm_get_submatrix(negf_env%h_sc(1, icontact), target_m)
    1258            0 :                IF (para_env%is_source()) CALL negf_print_matrix_to_file(filename_hc_1(icontact), target_m)
    1259            0 :                IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_sc is saved to "//TRIM(filename_hc_1(icontact))//" for spin 1"
    1260            0 :                CALL cp_fm_get_submatrix(negf_env%h_sc(2, icontact), target_m)
    1261            0 :                IF (para_env%is_source()) CALL negf_print_matrix_to_file(filename_hc_2(icontact), target_m)
    1262            0 :                IF (log_unit > 0) WRITE (log_unit, '(T2,A)') "H_sc is saved to "//TRIM(filename_hc_2(icontact))//" for spin 2"
    1263              :             END IF
    1264            0 :             DEALLOCATE (target_m)
    1265              :          END DO
    1266              : 
    1267            0 :          negf_control%write_common_restart_file = .TRUE.
    1268              : 
    1269              :       END IF
    1270              : 
    1271            0 :       DEALLOCATE (filename_sc, filename_hc_1, filename_hc_2)
    1272            0 :       CALL timestop(handle)
    1273            0 :    END SUBROUTINE negf_env_scatt_read_write_hs
    1274              : 
    1275              : ! **************************************************************************************************
    1276              : !> \brief Extract relevant matrix blocks for the scattering region as well as
    1277              : !>        all the scattering -- contact interface regions.
    1278              : !> \param negf_env            NEGF environment (modified on exit)
    1279              : !> \param negf_control        NEGF control
    1280              : !> \param sub_env             NEGF parallel (sub)group environment
    1281              : !> \param qs_env              Primary QuickStep environment
    1282              : !> \author Sergey Chulkov
    1283              : ! **************************************************************************************************
    1284            4 :    SUBROUTINE negf_env_device_init_matrices(negf_env, negf_control, sub_env, qs_env)
    1285              :       TYPE(negf_env_type), INTENT(inout)                 :: negf_env
    1286              :       TYPE(negf_control_type), POINTER                   :: negf_control
    1287              :       TYPE(negf_subgroup_env_type), INTENT(in)           :: sub_env
    1288              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1289              : 
    1290              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'negf_env_device_init_matrices'
    1291              : 
    1292              :       INTEGER                                            :: handle, icontact, ispin, nao_c, nao_s, &
    1293              :                                                             ncontacts, nspins
    1294              :       LOGICAL                                            :: do_kpoints
    1295              :       TYPE(cp_fm_struct_type), POINTER                   :: fm_struct
    1296              :       TYPE(dbcsr_p_type)                                 :: hmat
    1297            4 :       TYPE(dbcsr_p_type), DIMENSION(:, :), POINTER       :: matrix_ks_kp, matrix_s_kp
    1298              :       TYPE(dft_control_type), POINTER                    :: dft_control
    1299              :       TYPE(mp_para_env_type), POINTER                    :: para_env
    1300              :       TYPE(pw_env_type), POINTER                         :: pw_env
    1301              :       TYPE(pw_pool_type), POINTER                        :: pw_pool
    1302              :       TYPE(pw_r3d_rs_type)                               :: v_hartree
    1303              :       TYPE(qs_subsys_type), POINTER                      :: subsys
    1304              : 
    1305            4 :       CALL timeset(routineN, handle)
    1306              : 
    1307            4 :       IF (ALLOCATED(negf_control%atomlist_S_screening)) THEN
    1308              :          CALL get_qs_env(qs_env, &
    1309              :                          dft_control=dft_control, &
    1310              :                          do_kpoints=do_kpoints, &
    1311              :                          matrix_ks_kp=matrix_ks_kp, &
    1312              :                          matrix_s_kp=matrix_s_kp, &
    1313              :                          para_env=para_env, &
    1314              :                          pw_env=pw_env, &
    1315            4 :                          subsys=subsys)
    1316            4 :          CALL pw_env_get(pw_env, auxbas_pw_pool=pw_pool)
    1317              : 
    1318            4 :          IF (do_kpoints) THEN
    1319              :             CALL cp_abort(__LOCATION__, &
    1320            0 :                           "K-points in device region have not been implemented yet.")
    1321              :          END IF
    1322              : 
    1323            4 :          ncontacts = SIZE(negf_control%contacts)
    1324            4 :          nspins = dft_control%nspins
    1325              : 
    1326            4 :          NULLIFY (fm_struct)
    1327            4 :          nao_s = number_of_atomic_orbitals(subsys, negf_control%atomlist_S_screening)
    1328              : 
    1329              :          ! ++ create matrices: h_s, s_s
    1330            4 :          NULLIFY (negf_env%s_s, negf_env%v_hartree_s, fm_struct)
    1331           16 :          ALLOCATE (negf_env%h_s(nspins))
    1332              : 
    1333            4 :          CALL cp_fm_struct_create(fm_struct, nrow_global=nao_s, ncol_global=nao_s, context=sub_env%blacs_env)
    1334            4 :          ALLOCATE (negf_env%s_s)
    1335            4 :          CALL cp_fm_create(negf_env%s_s, fm_struct)
    1336            8 :          DO ispin = 1, nspins
    1337            8 :             CALL cp_fm_create(negf_env%h_s(ispin), fm_struct)
    1338              :          END DO
    1339            4 :          ALLOCATE (negf_env%v_hartree_s)
    1340            4 :          CALL cp_fm_create(negf_env%v_hartree_s, fm_struct)
    1341            4 :          CALL cp_fm_struct_release(fm_struct)
    1342              : 
    1343              :          ! ++ create matrices: h_sc, s_sc
    1344           48 :          ALLOCATE (negf_env%h_sc(nspins, ncontacts), negf_env%s_sc(ncontacts))
    1345           12 :          DO icontact = 1, ncontacts
    1346            8 :             nao_c = number_of_atomic_orbitals(subsys, negf_env%contacts(icontact)%atomlist_cell0)
    1347            8 :             CALL cp_fm_struct_create(fm_struct, nrow_global=nao_s, ncol_global=nao_c, context=sub_env%blacs_env)
    1348              : 
    1349            8 :             CALL cp_fm_create(negf_env%s_sc(icontact), fm_struct)
    1350              : 
    1351           16 :             DO ispin = 1, nspins
    1352           16 :                CALL cp_fm_create(negf_env%h_sc(ispin, icontact), fm_struct)
    1353              :             END DO
    1354              : 
    1355           12 :             CALL cp_fm_struct_release(fm_struct)
    1356              :          END DO
    1357              : 
    1358              :          ! extract matrices: h_s, s_s
    1359            8 :          DO ispin = 1, nspins
    1360              :             CALL negf_copy_sym_dbcsr_to_fm_submat(matrix=matrix_ks_kp(ispin, 1)%matrix, &
    1361              :                                                   fm=negf_env%h_s(ispin), &
    1362              :                                                   atomlist_row=negf_control%atomlist_S_screening, &
    1363              :                                                   atomlist_col=negf_control%atomlist_S_screening, &
    1364              :                                                   subsys=subsys, mpi_comm_global=para_env, &
    1365            8 :                                                   do_upper_diag=.TRUE., do_lower=.TRUE.)
    1366              :          END DO
    1367              : 
    1368              :          CALL negf_copy_sym_dbcsr_to_fm_submat(matrix=matrix_s_kp(1, 1)%matrix, &
    1369              :                                                fm=negf_env%s_s, &
    1370              :                                                atomlist_row=negf_control%atomlist_S_screening, &
    1371              :                                                atomlist_col=negf_control%atomlist_S_screening, &
    1372              :                                                subsys=subsys, mpi_comm_global=para_env, &
    1373            4 :                                                do_upper_diag=.TRUE., do_lower=.TRUE.)
    1374              : 
    1375              :          ! v_hartree_s
    1376            4 :          NULLIFY (hmat%matrix)
    1377            4 :          CALL dbcsr_init_p(hmat%matrix)
    1378            4 :          CALL dbcsr_copy(matrix_b=hmat%matrix, matrix_a=matrix_s_kp(1, 1)%matrix)
    1379            4 :          CALL dbcsr_set(hmat%matrix, 0.0_dp)
    1380              : 
    1381            4 :          CALL pw_pool%create_pw(v_hartree)
    1382            4 :          CALL negf_env_init_v_hartree(v_hartree, negf_env%contacts, negf_control%contacts)
    1383              : 
    1384              :          CALL integrate_v_rspace(v_rspace=v_hartree, hmat=hmat, qs_env=qs_env, &
    1385            4 :                                  calculate_forces=.FALSE., compute_tau=.FALSE., gapw=.FALSE.)
    1386              : 
    1387            4 :          CALL pw_pool%give_back_pw(v_hartree)
    1388              : 
    1389              :          CALL negf_copy_sym_dbcsr_to_fm_submat(matrix=hmat%matrix, &
    1390              :                                                fm=negf_env%v_hartree_s, &
    1391              :                                                atomlist_row=negf_control%atomlist_S_screening, &
    1392              :                                                atomlist_col=negf_control%atomlist_S_screening, &
    1393              :                                                subsys=subsys, mpi_comm_global=para_env, &
    1394            4 :                                                do_upper_diag=.TRUE., do_lower=.TRUE.)
    1395              : 
    1396            4 :          CALL dbcsr_deallocate_matrix(hmat%matrix)
    1397              : 
    1398              :          ! extract matrices: h_sc, s_sc
    1399           12 :          DO icontact = 1, ncontacts
    1400           16 :             DO ispin = 1, nspins
    1401              :                CALL negf_copy_sym_dbcsr_to_fm_submat(matrix=matrix_ks_kp(ispin, 1)%matrix, &
    1402              :                                                      fm=negf_env%h_sc(ispin, icontact), &
    1403              :                                                      atomlist_row=negf_control%atomlist_S_screening, &
    1404              :                                                      atomlist_col=negf_env%contacts(icontact)%atomlist_cell0, &
    1405              :                                                      subsys=subsys, mpi_comm_global=para_env, &
    1406           16 :                                                      do_upper_diag=.TRUE., do_lower=.TRUE.)
    1407              :             END DO
    1408              : 
    1409              :             CALL negf_copy_sym_dbcsr_to_fm_submat(matrix=matrix_s_kp(1, 1)%matrix, &
    1410              :                                                   fm=negf_env%s_sc(icontact), &
    1411              :                                                   atomlist_row=negf_control%atomlist_S_screening, &
    1412              :                                                   atomlist_col=negf_env%contacts(icontact)%atomlist_cell0, &
    1413              :                                                   subsys=subsys, mpi_comm_global=para_env, &
    1414           12 :                                                   do_upper_diag=.TRUE., do_lower=.TRUE.)
    1415              :          END DO
    1416              :       END IF
    1417              : 
    1418            4 :       CALL timestop(handle)
    1419            4 :    END SUBROUTINE negf_env_device_init_matrices
    1420              : 
    1421              : ! **************************************************************************************************
    1422              : !> \brief Contribution to the Hartree potential related to the external bias voltage.
    1423              : !> \param v_hartree        Hartree potential (modified on exit)
    1424              : !> \param contact_env      NEGF environment for every contact
    1425              : !> \param contact_control  NEGF control for every contact
    1426              : !> \author Sergey Chulkov
    1427              : ! **************************************************************************************************
    1428            4 :    SUBROUTINE negf_env_init_v_hartree(v_hartree, contact_env, contact_control)
    1429              :       TYPE(pw_r3d_rs_type), INTENT(IN)                   :: v_hartree
    1430              :       TYPE(negf_env_contact_type), DIMENSION(:), &
    1431              :          INTENT(in)                                      :: contact_env
    1432              :       TYPE(negf_control_contact_type), DIMENSION(:), &
    1433              :          INTENT(in)                                      :: contact_control
    1434              : 
    1435              :       CHARACTER(len=*), PARAMETER :: routineN = 'negf_env_init_v_hartree'
    1436              :       REAL(kind=dp), PARAMETER :: threshold = 16.0_dp*EPSILON(0.0_dp)
    1437              : 
    1438              :       INTEGER                                            :: dx, dy, dz, handle, icontact, ix, iy, &
    1439              :                                                             iz, lx, ly, lz, ncontacts, ux, uy, uz
    1440              :       REAL(kind=dp)                                      :: dvol, pot, proj, v1, v2
    1441              :       REAL(kind=dp), DIMENSION(3)                        :: dirvector_bias, point_coord, &
    1442              :                                                             point_indices, vector
    1443              : 
    1444            4 :       CALL timeset(routineN, handle)
    1445              : 
    1446            4 :       ncontacts = SIZE(contact_env)
    1447            4 :       CPASSERT(SIZE(contact_control) == ncontacts)
    1448            4 :       CPASSERT(ncontacts == 2)
    1449              : 
    1450           16 :       dirvector_bias = contact_env(2)%origin_bias - contact_env(1)%origin_bias
    1451            4 :       v1 = contact_control(1)%v_external
    1452            4 :       v2 = contact_control(2)%v_external
    1453              : 
    1454            4 :       lx = v_hartree%pw_grid%bounds_local(1, 1)
    1455            4 :       ux = v_hartree%pw_grid%bounds_local(2, 1)
    1456            4 :       ly = v_hartree%pw_grid%bounds_local(1, 2)
    1457            4 :       uy = v_hartree%pw_grid%bounds_local(2, 2)
    1458            4 :       lz = v_hartree%pw_grid%bounds_local(1, 3)
    1459            4 :       uz = v_hartree%pw_grid%bounds_local(2, 3)
    1460              : 
    1461            4 :       dx = v_hartree%pw_grid%npts(1)/2
    1462            4 :       dy = v_hartree%pw_grid%npts(2)/2
    1463            4 :       dz = v_hartree%pw_grid%npts(3)/2
    1464              : 
    1465            4 :       dvol = v_hartree%pw_grid%dvol
    1466              : 
    1467         1028 :       DO iz = lz, uz
    1468         1024 :          point_indices(3) = REAL(iz + dz, kind=dp)
    1469        41988 :          DO iy = ly, uy
    1470        40960 :             point_indices(2) = REAL(iy + dy, kind=dp)
    1471              : 
    1472       861184 :             DO ix = lx, ux
    1473       819200 :                point_indices(1) = REAL(ix + dx, kind=dp)
    1474     10649600 :                point_coord(:) = MATMUL(v_hartree%pw_grid%dh, point_indices)
    1475              : 
    1476      3276800 :                vector = point_coord - contact_env(1)%origin_bias
    1477       819200 :                proj = projection_on_direction_vector(vector, dirvector_bias)
    1478       819200 :                IF (proj + threshold >= 0.0_dp .AND. proj - threshold <= 1.0_dp) THEN
    1479              :                   ! scattering region
    1480              :                   ! proj == 0   we are at the first contact boundary
    1481              :                   ! proj == 1   we are at the second contact boundary
    1482       320000 :                   IF (proj < 0.0_dp) THEN
    1483              :                      proj = 0.0_dp
    1484       320000 :                   ELSE IF (proj > 1.0_dp) THEN
    1485            0 :                      proj = 1.0_dp
    1486              :                   END IF
    1487       320000 :                   pot = v1 + (v2 - v1)*proj
    1488              :                ELSE
    1489       790400 :                   pot = 0.0_dp
    1490       790400 :                   DO icontact = 1, ncontacts
    1491      3046400 :                      vector = point_coord - contact_env(icontact)%origin_bias
    1492       761600 :                      proj = projection_on_direction_vector(vector, contact_env(icontact)%direction_vector_bias)
    1493              : 
    1494       790400 :                      IF (proj + threshold >= 0.0_dp .AND. proj - threshold <= 1.0_dp) THEN
    1495       470400 :                         pot = contact_control(icontact)%v_external
    1496       470400 :                         EXIT
    1497              :                      END IF
    1498              :                   END DO
    1499              :                END IF
    1500              : 
    1501       860160 :                v_hartree%array(ix, iy, iz) = pot*dvol
    1502              :             END DO
    1503              :          END DO
    1504              :       END DO
    1505              : 
    1506            4 :       CALL timestop(handle)
    1507            4 :    END SUBROUTINE negf_env_init_v_hartree
    1508              : 
    1509              : ! **************************************************************************************************
    1510              : !> \brief Detect the axis towards secondary unit cell.
    1511              : !> \param direction_vector    direction vector
    1512              : !> \param subsys_contact      QuickStep subsystem of the contact force environment
    1513              : !> \param eps_geometry        accuracy in mapping atoms between different force environments
    1514              : !> \return direction axis: 0 (undefined), 1 (x), 2(y), 3 (z)
    1515              : !> \par History
    1516              : !>   * 08.2017 created [Sergey Chulkov]
    1517              : ! **************************************************************************************************
    1518            8 :    FUNCTION contact_direction_axis(direction_vector, subsys_contact, eps_geometry) RESULT(direction_axis)
    1519              :       REAL(kind=dp), DIMENSION(3), INTENT(in)            :: direction_vector
    1520              :       TYPE(qs_subsys_type), POINTER                      :: subsys_contact
    1521              :       REAL(kind=dp), INTENT(in)                          :: eps_geometry
    1522              :       INTEGER                                            :: direction_axis
    1523              : 
    1524              :       INTEGER                                            :: i, naxes
    1525              :       REAL(kind=dp), DIMENSION(3)                        :: scaled
    1526              :       TYPE(cell_type), POINTER                           :: cell
    1527              : 
    1528            8 :       CALL qs_subsys_get(subsys_contact, cell=cell)
    1529            8 :       CALL real_to_scaled(scaled, direction_vector, cell)
    1530              : 
    1531            8 :       naxes = 0
    1532            8 :       direction_axis = 0 ! initialize to make GCC<=6 happy
    1533              : 
    1534           32 :       DO i = 1, 3
    1535           32 :          IF (ABS(scaled(i)) > eps_geometry) THEN
    1536            8 :             IF (scaled(i) > 0.0_dp) THEN
    1537              :                direction_axis = i
    1538              :             ELSE
    1539            4 :                direction_axis = -i
    1540              :             END IF
    1541            8 :             naxes = naxes + 1
    1542              :          END IF
    1543              :       END DO
    1544              : 
    1545              :       ! direction_vector is not parallel to one of the unit cell's axis
    1546            8 :       IF (naxes /= 1) direction_axis = 0
    1547            8 :    END FUNCTION contact_direction_axis
    1548              : 
    1549              : ! **************************************************************************************************
    1550              : !> \brief Estimate energy of the highest spin-alpha occupied molecular orbital.
    1551              : !> \param homo_energy  HOMO energy (initialised on exit)
    1552              : !> \param qs_env       QuickStep environment
    1553              : !> \par History
    1554              : !>   * 01.2017 created [Sergey Chulkov]
    1555              : ! **************************************************************************************************
    1556            4 :    SUBROUTINE negf_homo_energy_estimate(homo_energy, qs_env)
    1557              :       REAL(kind=dp), INTENT(out)                         :: homo_energy
    1558              :       TYPE(qs_environment_type), POINTER                 :: qs_env
    1559              : 
    1560              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'negf_homo_energy_estimate'
    1561              :       INTEGER, PARAMETER                                 :: gamma_point = 1
    1562              : 
    1563              :       INTEGER                                            :: handle, homo, ikpgr, ikpoint, imo, &
    1564              :                                                             ispin, kplocal, nmo, nspins
    1565              :       INTEGER, DIMENSION(2)                              :: kp_range
    1566              :       LOGICAL                                            :: do_kpoints
    1567              :       REAL(kind=dp)                                      :: my_homo_energy
    1568            4 :       REAL(kind=dp), DIMENSION(:), POINTER               :: eigenvalues
    1569            4 :       TYPE(kpoint_env_p_type), DIMENSION(:), POINTER     :: kp_env
    1570              :       TYPE(kpoint_type), POINTER                         :: kpoints
    1571            4 :       TYPE(mo_set_type), DIMENSION(:), POINTER           :: mos
    1572            4 :       TYPE(mo_set_type), DIMENSION(:, :), POINTER        :: mos_kp
    1573              :       TYPE(mp_para_env_type), POINTER                    :: para_env, para_env_kp
    1574              : 
    1575            4 :       CALL timeset(routineN, handle)
    1576            4 :       my_homo_energy = 0.0_dp
    1577              : 
    1578            4 :       CALL get_qs_env(qs_env, para_env=para_env, mos=mos, kpoints=kpoints, do_kpoints=do_kpoints)
    1579              : 
    1580            4 :       IF (do_kpoints) THEN
    1581            4 :          CALL get_kpoint_info(kpoints, kp_env=kp_env, kp_range=kp_range, para_env_kp=para_env_kp)
    1582              : 
    1583              :          ! looking for a processor that holds the gamma point
    1584            4 :          IF (para_env_kp%mepos == 0 .AND. kp_range(1) <= gamma_point .AND. kp_range(2) >= gamma_point) THEN
    1585            2 :             kplocal = kp_range(2) - kp_range(1) + 1
    1586              : 
    1587            2 :             DO ikpgr = 1, kplocal
    1588            2 :                CALL get_kpoint_env(kp_env(ikpgr)%kpoint_env, nkpoint=ikpoint, mos=mos_kp)
    1589              : 
    1590            2 :                IF (ikpoint == gamma_point) THEN
    1591              :                   ! mos_kp(component, spin), where component = 1 (real), or 2 (imaginary)
    1592            2 :                   CALL get_mo_set(mos_kp(1, 1), homo=homo, eigenvalues=eigenvalues) ! mu=fermi_level
    1593              : 
    1594            2 :                   my_homo_energy = eigenvalues(homo)
    1595            2 :                   EXIT
    1596              :                END IF
    1597              :             END DO
    1598              :          END IF
    1599              : 
    1600            4 :          CALL para_env%sum(my_homo_energy)
    1601              :       ELSE
    1602              :          ! Hamiltonian of the bulk contact region has been computed without k-points.
    1603              :          ! Try to obtain the HOMO energy assuming there is no OT. We probably should abort here
    1604              :          ! as we do need a second replica of the bulk contact unit cell along transport
    1605              :          ! direction anyway which is not available without k-points.
    1606              : 
    1607              :          CALL cp_abort(__LOCATION__, &
    1608              :                        "It is necessary to use k-points along the transport direction "// &
    1609            0 :                        "for all contact FORCE_EVAL-s")
    1610              :          !      It is necessary to use k-points along the transport direction within all contact FORCE_EVAL-s
    1611              : 
    1612            0 :          nspins = SIZE(mos)
    1613              : 
    1614            0 :          spin_loop: DO ispin = 1, nspins
    1615            0 :             CALL get_mo_set(mos(ispin), homo=homo, nmo=nmo, eigenvalues=eigenvalues)
    1616              : 
    1617            0 :             DO imo = nmo, 1, -1
    1618            0 :                IF (eigenvalues(imo) /= 0.0_dp) EXIT spin_loop
    1619              :             END DO
    1620              :          END DO spin_loop
    1621              : 
    1622            0 :          IF (imo == 0) THEN
    1623            0 :             CPABORT("Orbital transformation (OT) for contact FORCE_EVAL-s is not supported")
    1624              :          END IF
    1625              : 
    1626            0 :          my_homo_energy = eigenvalues(homo)
    1627              :       END IF
    1628              : 
    1629            4 :       homo_energy = my_homo_energy
    1630            4 :       CALL timestop(handle)
    1631            4 :    END SUBROUTINE negf_homo_energy_estimate
    1632              : 
    1633              : ! **************************************************************************************************
    1634              : !> \brief List atoms from the contact's primary unit cell.
    1635              : !> \param atomlist_cell0    list of atoms belonging to the contact's primary unit cell
    1636              : !>                          (allocate and initialised on exit)
    1637              : !> \param atom_map_cell0    atomic map of atoms from 'atomlist_cell0' (allocate and initialised on exit)
    1638              : !> \param atomlist_bulk     list of atoms belonging to the bulk contact region
    1639              : !> \param atom_map          atomic map of atoms from 'atomlist_bulk'
    1640              : !> \param origin            origin of the contact
    1641              : !> \param direction_vector  direction vector of the contact
    1642              : !> \param direction_axis    axis towards secondary unit cell
    1643              : !> \param subsys_device     QuickStep subsystem of the device force environment
    1644              : !> \par History
    1645              : !>   * 08.2017 created [Sergey Chulkov]
    1646              : ! **************************************************************************************************
    1647            4 :    SUBROUTINE list_atoms_in_bulk_primary_unit_cell(atomlist_cell0, atom_map_cell0, atomlist_bulk, atom_map, &
    1648              :                                                    origin, direction_vector, direction_axis, subsys_device)
    1649              :       INTEGER, ALLOCATABLE, DIMENSION(:), INTENT(inout)  :: atomlist_cell0
    1650              :       TYPE(negf_atom_map_type), ALLOCATABLE, &
    1651              :          DIMENSION(:), INTENT(inout)                     :: atom_map_cell0
    1652              :       INTEGER, DIMENSION(:), INTENT(in)                  :: atomlist_bulk
    1653              :       TYPE(negf_atom_map_type), DIMENSION(:), INTENT(in) :: atom_map
    1654              :       REAL(kind=dp), DIMENSION(3), INTENT(in)            :: origin, direction_vector
    1655              :       INTEGER, INTENT(in)                                :: direction_axis
    1656              :       TYPE(qs_subsys_type), POINTER                      :: subsys_device
    1657              : 
    1658              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'list_atoms_in_bulk_primary_unit_cell'
    1659              : 
    1660              :       INTEGER                                            :: atom_min, dir_axis_min, &
    1661              :                                                             direction_axis_abs, handle, iatom, &
    1662              :                                                             natoms_bulk, natoms_cell0
    1663              :       REAL(kind=dp)                                      :: proj, proj_min
    1664              :       REAL(kind=dp), DIMENSION(3)                        :: vector
    1665            4 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1666              : 
    1667            4 :       CALL timeset(routineN, handle)
    1668            4 :       CALL qs_subsys_get(subsys_device, particle_set=particle_set)
    1669              : 
    1670            4 :       natoms_bulk = SIZE(atomlist_bulk)
    1671            4 :       CPASSERT(SIZE(atom_map, 1) == natoms_bulk)
    1672            4 :       direction_axis_abs = ABS(direction_axis)
    1673              : 
    1674              :       ! looking for the nearest atom from the scattering region
    1675            4 :       proj_min = 1.0_dp
    1676            4 :       atom_min = 1
    1677           36 :       DO iatom = 1, natoms_bulk
    1678          128 :          vector = particle_set(atomlist_bulk(iatom))%r - origin
    1679           32 :          proj = projection_on_direction_vector(vector, direction_vector)
    1680              : 
    1681           36 :          IF (proj < proj_min) THEN
    1682           16 :             proj_min = proj
    1683           16 :             atom_min = iatom
    1684              :          END IF
    1685              :       END DO
    1686              : 
    1687            4 :       dir_axis_min = atom_map(atom_min)%cell(direction_axis_abs)
    1688              : 
    1689            4 :       natoms_cell0 = 0
    1690           36 :       DO iatom = 1, natoms_bulk
    1691           32 :          IF (atom_map(iatom)%cell(direction_axis_abs) == dir_axis_min) &
    1692           20 :             natoms_cell0 = natoms_cell0 + 1
    1693              :       END DO
    1694              : 
    1695           12 :       ALLOCATE (atomlist_cell0(natoms_cell0))
    1696           40 :       ALLOCATE (atom_map_cell0(natoms_cell0))
    1697              : 
    1698            4 :       natoms_cell0 = 0
    1699           36 :       DO iatom = 1, natoms_bulk
    1700           36 :          IF (atom_map(iatom)%cell(direction_axis_abs) == dir_axis_min) THEN
    1701           16 :             natoms_cell0 = natoms_cell0 + 1
    1702           16 :             atomlist_cell0(natoms_cell0) = atomlist_bulk(iatom)
    1703           16 :             atom_map_cell0(natoms_cell0) = atom_map(iatom)
    1704              :          END IF
    1705              :       END DO
    1706              : 
    1707            4 :       CALL timestop(handle)
    1708            4 :    END SUBROUTINE list_atoms_in_bulk_primary_unit_cell
    1709              : 
    1710              : ! **************************************************************************************************
    1711              : !> \brief List atoms from the contact's secondary unit cell.
    1712              : !> \param atomlist_cell1    list of atoms belonging to the contact's secondary unit cell
    1713              : !>                          (allocate and initialised on exit)
    1714              : !> \param atom_map_cell1    atomic map of atoms from 'atomlist_cell1'
    1715              : !>                          (allocate and initialised on exit)
    1716              : !> \param atomlist_bulk     list of atoms belonging to the bulk contact region
    1717              : !> \param atom_map          atomic map of atoms from 'atomlist_bulk'
    1718              : !> \param origin            origin of the contact
    1719              : !> \param direction_vector  direction vector of the contact
    1720              : !> \param direction_axis    axis towards the secondary unit cell
    1721              : !> \param subsys_device     QuickStep subsystem of the device force environment
    1722              : !> \par History
    1723              : !>   * 11.2017 created [Sergey Chulkov]
    1724              : !> \note Cloned from list_atoms_in_bulk_primary_unit_cell. Will be removed once we can managed to
    1725              : !>        maintain consistency between real-space matrices from different force_eval sections.
    1726              : ! **************************************************************************************************
    1727            4 :    SUBROUTINE list_atoms_in_bulk_secondary_unit_cell(atomlist_cell1, atom_map_cell1, atomlist_bulk, atom_map, &
    1728              :                                                      origin, direction_vector, direction_axis, subsys_device)
    1729              :       INTEGER, ALLOCATABLE, DIMENSION(:), INTENT(inout)  :: atomlist_cell1
    1730              :       TYPE(negf_atom_map_type), ALLOCATABLE, &
    1731              :          DIMENSION(:), INTENT(inout)                     :: atom_map_cell1
    1732              :       INTEGER, DIMENSION(:), INTENT(in)                  :: atomlist_bulk
    1733              :       TYPE(negf_atom_map_type), DIMENSION(:), INTENT(in) :: atom_map
    1734              :       REAL(kind=dp), DIMENSION(3), INTENT(in)            :: origin, direction_vector
    1735              :       INTEGER, INTENT(in)                                :: direction_axis
    1736              :       TYPE(qs_subsys_type), POINTER                      :: subsys_device
    1737              : 
    1738              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'list_atoms_in_bulk_secondary_unit_cell'
    1739              : 
    1740              :       INTEGER                                            :: atom_min, dir_axis_min, &
    1741              :                                                             direction_axis_abs, handle, iatom, &
    1742              :                                                             natoms_bulk, natoms_cell1, offset
    1743              :       REAL(kind=dp)                                      :: proj, proj_min
    1744              :       REAL(kind=dp), DIMENSION(3)                        :: vector
    1745            4 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
    1746              : 
    1747            4 :       CALL timeset(routineN, handle)
    1748            4 :       CALL qs_subsys_get(subsys_device, particle_set=particle_set)
    1749              : 
    1750            4 :       natoms_bulk = SIZE(atomlist_bulk)
    1751            4 :       CPASSERT(SIZE(atom_map, 1) == natoms_bulk)
    1752            4 :       direction_axis_abs = ABS(direction_axis)
    1753            4 :       offset = SIGN(1, direction_axis)
    1754              : 
    1755              :       ! looking for the nearest atom from the scattering region
    1756            4 :       proj_min = 1.0_dp
    1757            4 :       atom_min = 1
    1758           36 :       DO iatom = 1, natoms_bulk
    1759          128 :          vector = particle_set(atomlist_bulk(iatom))%r - origin
    1760           32 :          proj = projection_on_direction_vector(vector, direction_vector)
    1761              : 
    1762           36 :          IF (proj < proj_min) THEN
    1763           16 :             proj_min = proj
    1764           16 :             atom_min = iatom
    1765              :          END IF
    1766              :       END DO
    1767              : 
    1768            4 :       dir_axis_min = atom_map(atom_min)%cell(direction_axis_abs)
    1769              : 
    1770            4 :       natoms_cell1 = 0
    1771           36 :       DO iatom = 1, natoms_bulk
    1772           32 :          IF (atom_map(iatom)%cell(direction_axis_abs) == dir_axis_min + offset) &
    1773           20 :             natoms_cell1 = natoms_cell1 + 1
    1774              :       END DO
    1775              : 
    1776           12 :       ALLOCATE (atomlist_cell1(natoms_cell1))
    1777           40 :       ALLOCATE (atom_map_cell1(natoms_cell1))
    1778              : 
    1779            4 :       natoms_cell1 = 0
    1780           36 :       DO iatom = 1, natoms_bulk
    1781           36 :          IF (atom_map(iatom)%cell(direction_axis_abs) == dir_axis_min + offset) THEN
    1782           16 :             natoms_cell1 = natoms_cell1 + 1
    1783           16 :             atomlist_cell1(natoms_cell1) = atomlist_bulk(iatom)
    1784           16 :             atom_map_cell1(natoms_cell1) = atom_map(iatom)
    1785           16 :             atom_map_cell1(natoms_cell1)%cell(direction_axis_abs) = dir_axis_min
    1786              :          END IF
    1787              :       END DO
    1788              : 
    1789            4 :       CALL timestop(handle)
    1790            4 :    END SUBROUTINE list_atoms_in_bulk_secondary_unit_cell
    1791              : 
    1792              : ! **************************************************************************************************
    1793              : !> \brief Release a NEGF environment variable.
    1794              : !> \param negf_env  NEGF environment to release
    1795              : !> \par History
    1796              : !>   * 01.2017 created [Sergey Chulkov]
    1797              : ! **************************************************************************************************
    1798            4 :    SUBROUTINE negf_env_release(negf_env)
    1799              :       TYPE(negf_env_type), INTENT(inout)                 :: negf_env
    1800              : 
    1801              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'negf_env_release'
    1802              : 
    1803              :       INTEGER                                            :: handle, icontact
    1804              : 
    1805            4 :       CALL timeset(routineN, handle)
    1806              : 
    1807            4 :       IF (ALLOCATED(negf_env%contacts)) THEN
    1808           12 :          DO icontact = SIZE(negf_env%contacts), 1, -1
    1809           12 :             CALL negf_env_contact_release(negf_env%contacts(icontact))
    1810              :          END DO
    1811              : 
    1812           12 :          DEALLOCATE (negf_env%contacts)
    1813              :       END IF
    1814              : 
    1815              :       ! h_s
    1816            4 :       CALL cp_fm_release(negf_env%h_s)
    1817              : 
    1818              :       ! h_sc
    1819            4 :       CALL cp_fm_release(negf_env%h_sc)
    1820              : 
    1821              :       ! s_s
    1822            4 :       IF (ASSOCIATED(negf_env%s_s)) THEN
    1823            4 :          CALL cp_fm_release(negf_env%s_s)
    1824            4 :          DEALLOCATE (negf_env%s_s)
    1825              :          NULLIFY (negf_env%s_s)
    1826              :       END IF
    1827              : 
    1828              :       ! s_sc
    1829            4 :       CALL cp_fm_release(negf_env%s_sc)
    1830              : 
    1831              :       ! v_hartree_s
    1832            4 :       IF (ASSOCIATED(negf_env%v_hartree_s)) THEN
    1833            4 :          CALL cp_fm_release(negf_env%v_hartree_s)
    1834            4 :          DEALLOCATE (negf_env%v_hartree_s)
    1835              :          NULLIFY (negf_env%v_hartree_s)
    1836              :       END IF
    1837              : 
    1838              :       ! density mixing
    1839            4 :       IF (ASSOCIATED(negf_env%mixing_storage)) THEN
    1840            4 :          CALL mixing_storage_release(negf_env%mixing_storage)
    1841            4 :          DEALLOCATE (negf_env%mixing_storage)
    1842              :       END IF
    1843              : 
    1844            4 :       CALL timestop(handle)
    1845            4 :    END SUBROUTINE negf_env_release
    1846              : 
    1847              : ! **************************************************************************************************
    1848              : !> \brief Release a NEGF contact environment variable.
    1849              : !> \param contact_env  NEGF contact environment to release
    1850              : ! **************************************************************************************************
    1851            8 :    SUBROUTINE negf_env_contact_release(contact_env)
    1852              :       TYPE(negf_env_contact_type), INTENT(inout)         :: contact_env
    1853              : 
    1854              :       CHARACTER(len=*), PARAMETER :: routineN = 'negf_env_contact_release'
    1855              : 
    1856              :       INTEGER                                            :: handle
    1857              : 
    1858            8 :       CALL timeset(routineN, handle)
    1859              : 
    1860              :       ! h_00
    1861            8 :       CALL cp_fm_release(contact_env%h_00)
    1862              : 
    1863              :       ! h_01
    1864            8 :       CALL cp_fm_release(contact_env%h_01)
    1865              : 
    1866              :       ! rho_00
    1867            8 :       CALL cp_fm_release(contact_env%rho_00)
    1868              : 
    1869              :       ! rho_01
    1870            8 :       CALL cp_fm_release(contact_env%rho_01)
    1871              : 
    1872              :       ! s_00
    1873            8 :       IF (ASSOCIATED(contact_env%s_00)) THEN
    1874            8 :          CALL cp_fm_release(contact_env%s_00)
    1875            8 :          DEALLOCATE (contact_env%s_00)
    1876              :          NULLIFY (contact_env%s_00)
    1877              :       END IF
    1878              : 
    1879              :       ! s_01
    1880            8 :       IF (ASSOCIATED(contact_env%s_01)) THEN
    1881            8 :          CALL cp_fm_release(contact_env%s_01)
    1882            8 :          DEALLOCATE (contact_env%s_01)
    1883              :          NULLIFY (contact_env%s_01)
    1884              :       END IF
    1885              : 
    1886            8 :       IF (ALLOCATED(contact_env%atomlist_cell0)) DEALLOCATE (contact_env%atomlist_cell0)
    1887            8 :       IF (ALLOCATED(contact_env%atomlist_cell1)) DEALLOCATE (contact_env%atomlist_cell1)
    1888            8 :       IF (ALLOCATED(contact_env%atom_map_cell0)) DEALLOCATE (contact_env%atom_map_cell0)
    1889              : 
    1890            8 :       CALL timestop(handle)
    1891            8 :    END SUBROUTINE negf_env_contact_release
    1892              : 
    1893            0 : END MODULE negf_env_types
        

Generated by: LCOV version 2.0-1