LCOV - code coverage report
Current view: top level - src - transport.F (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:561f475) Lines: 7.4 % 282 21
Test Date: 2026-06-21 06:48:54 Functions: 20.0 % 10 2

            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 routines for DFT+NEGF calculations (coupling with the quantum transport code OMEN)
      10              : !> \par History
      11              : !>       12.2012 created external_scf_method [Hossein Bani-Hashemian]
      12              : !>       05.2013 created rotines to work with C-interoperable matrices [Hossein Bani-Hashemian]
      13              : !>       07.2013 created transport_env routines [Hossein Bani-Hashemian]
      14              : !>       11.2014 switch to CSR matrices [Hossein Bani-Hashemian]
      15              : !>       12.2014 merged [Hossein Bani-Hashemian]
      16              : !>       04.2016 added imaginary DM and current density cube [Hossein Bani-Hashemian]
      17              : !> \author Mohammad Hossein Bani-Hashemian
      18              : ! **************************************************************************************************
      19              : MODULE transport
      20              :    USE ISO_C_BINDING,                   ONLY: C_ASSOCIATED,&
      21              :                                               C_BOOL,&
      22              :                                               C_DOUBLE,&
      23              :                                               C_F_PROCPOINTER,&
      24              :                                               C_INT,&
      25              :                                               C_LOC,&
      26              :                                               C_NULL_PTR,&
      27              :                                               C_PTR
      28              :    USE atomic_kind_types,               ONLY: atomic_kind_type,&
      29              :                                               get_atomic_kind
      30              :    USE bibliography,                    ONLY: Bruck2014,&
      31              :                                               cite_reference
      32              :    USE cp_control_types,                ONLY: dft_control_type
      33              :    USE cp_dbcsr_api,                    ONLY: &
      34              :         dbcsr_convert_csr_to_dbcsr, dbcsr_convert_dbcsr_to_csr, dbcsr_copy, dbcsr_create, &
      35              :         dbcsr_csr_create, dbcsr_csr_create_from_dbcsr, dbcsr_csr_dbcsr_blkrow_dist, &
      36              :         dbcsr_csr_print_sparsity, dbcsr_csr_type, dbcsr_deallocate_matrix, dbcsr_desymmetrize, &
      37              :         dbcsr_has_symmetry, dbcsr_set, dbcsr_type, dbcsr_type_no_symmetry
      38              :    USE cp_dbcsr_cp2k_link,              ONLY: cp_dbcsr_to_csr_screening
      39              :    USE cp_log_handling,                 ONLY: cp_get_default_logger,&
      40              :                                               cp_logger_get_default_unit_nr,&
      41              :                                               cp_logger_type
      42              :    USE cp_output_handling,              ONLY: cp_p_file,&
      43              :                                               cp_print_key_finished_output,&
      44              :                                               cp_print_key_should_output,&
      45              :                                               cp_print_key_unit_nr
      46              :    USE cp_realspace_grid_cube,          ONLY: cp_pw_to_cube
      47              :    USE input_section_types,             ONLY: section_get_ivals,&
      48              :                                               section_vals_get,&
      49              :                                               section_vals_get_subs_vals,&
      50              :                                               section_vals_type,&
      51              :                                               section_vals_val_get
      52              :    USE kinds,                           ONLY: dp
      53              :    USE particle_list_types,             ONLY: particle_list_type
      54              :    USE particle_methods,                ONLY: get_particle_set
      55              :    USE particle_types,                  ONLY: particle_type
      56              :    USE physcon,                         ONLY: boltzmann,&
      57              :                                               e_charge,&
      58              :                                               evolt,&
      59              :                                               h_bar
      60              :    USE pw_env_types,                    ONLY: pw_env_get,&
      61              :                                               pw_env_type
      62              :    USE pw_methods,                      ONLY: pw_zero
      63              :    USE pw_pool_types,                   ONLY: pw_pool_type
      64              :    USE pw_types,                        ONLY: pw_c1d_gs_type,&
      65              :                                               pw_r3d_rs_type
      66              :    USE qs_environment_types,            ONLY: get_qs_env,&
      67              :                                               qs_environment_type,&
      68              :                                               set_qs_env
      69              :    USE qs_kind_types,                   ONLY: get_qs_kind,&
      70              :                                               qs_kind_type
      71              :    USE qs_ks_types,                     ONLY: qs_ks_env_type
      72              :    USE qs_linres_current,               ONLY: calculate_jrho_resp
      73              :    USE qs_linres_types,                 ONLY: current_env_type
      74              :    USE qs_rho_types,                    ONLY: qs_rho_get,&
      75              :                                               qs_rho_type
      76              :    USE qs_subsys_types,                 ONLY: qs_subsys_get,&
      77              :                                               qs_subsys_type
      78              :    USE transport_env_types,             ONLY: cp2k_csr_interop_type,&
      79              :                                               cp2k_transport_parameters,&
      80              :                                               csr_interop_matrix_get_info,&
      81              :                                               csr_interop_nullify,&
      82              :                                               transport_env_type
      83              : #include "./base/base_uses.f90"
      84              : 
      85              :    IMPLICIT NONE
      86              :    PRIVATE
      87              : 
      88              :    CHARACTER(len=*), PARAMETER, PRIVATE :: moduleN = 'transport'
      89              : 
      90              :    PUBLIC :: transport_env_create, transport_initialize, external_scf_method
      91              :    PUBLIC :: qs_scf_post_transport
      92              : 
      93              : !> interface between C/C++ and FORTRAN
      94              :    INTERFACE c_func_interface
      95              : ! **************************************************************************************************
      96              : !> \brief C routine that takes the S and H matrices as input and outputs a P matrix
      97              : !> \param cp2k_transport_params transport parameters read form a CP2K input file
      98              : !> \param s_mat  C-interoperable overlap matrix
      99              : !> \param ks_mat C-interoperable Kohn-Sham matrix
     100              : !> \param p_mat  C-interoperable density matrix
     101              : !> \param imagp_mat C-interoperable imaginary density matrix
     102              : !> \author Mohammad Hossein Bani-Hashemian
     103              : ! **************************************************************************************************
     104              :       SUBROUTINE c_scf_routine(cp2k_transport_params, s_mat, ks_mat, p_mat, imagp_mat) BIND(C)
     105              :          IMPORT :: C_INT, C_PTR, cp2k_csr_interop_type, cp2k_transport_parameters
     106              :          TYPE(cp2k_transport_parameters), VALUE, INTENT(IN) :: cp2k_transport_params
     107              :          TYPE(cp2k_csr_interop_type), VALUE, INTENT(IN)     :: s_mat
     108              :          TYPE(cp2k_csr_interop_type), VALUE, INTENT(IN)     :: ks_mat
     109              :          TYPE(cp2k_csr_interop_type), INTENT(INOUT)         :: p_mat
     110              :          TYPE(cp2k_csr_interop_type), INTENT(INOUT)         :: imagp_mat
     111              :       END SUBROUTINE c_scf_routine
     112              :    END INTERFACE c_func_interface
     113              : 
     114              : CONTAINS
     115              : 
     116              : ! **************************************************************************************************
     117              : !> \brief creates the transport environment
     118              : !> \param[inout] qs_env the qs_env containing the transport_env
     119              : !> \author Mohammad Hossein Bani-Hashemian
     120              : ! **************************************************************************************************
     121            0 :    SUBROUTINE transport_env_create(qs_env)
     122              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     123              : 
     124              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'transport_env_create'
     125              : 
     126              :       INTEGER                                            :: handle
     127              :       TYPE(dft_control_type), POINTER                    :: dft_control
     128              :       TYPE(section_vals_type), POINTER                   :: input
     129              :       TYPE(transport_env_type), POINTER                  :: transport_env
     130              : 
     131            0 :       CALL timeset(routineN, handle)
     132              : 
     133              :       CALL get_qs_env(qs_env, &
     134              :                       transport_env=transport_env, &
     135              :                       dft_control=dft_control, &
     136            0 :                       input=input)
     137              : 
     138            0 :       CPASSERT(.NOT. ASSOCIATED(transport_env))
     139              : 
     140            0 :       ALLOCATE (transport_env)
     141              : 
     142            0 :       CALL transport_init_read_input(input, transport_env)
     143            0 :       CALL transport_set_contact_params(qs_env, transport_env)
     144              : 
     145            0 :       CALL set_qs_env(qs_env, transport_env=transport_env)
     146              : 
     147            0 :       CALL timestop(handle)
     148              : 
     149            0 :    END SUBROUTINE transport_env_create
     150              : 
     151              : ! **************************************************************************************************
     152              : !> \brief intitializes all fields of transport_env using the parameters read from
     153              : !>        the corresponding input section
     154              : !> \param[inout] input         the input file
     155              : !> \param[inout] transport_env the transport_env to be initialized
     156              : !> \author Mohammad Hossein Bani-Hashemian
     157              : ! **************************************************************************************************
     158            0 :    SUBROUTINE transport_init_read_input(input, transport_env)
     159              :       TYPE(section_vals_type), POINTER                   :: input
     160              :       TYPE(transport_env_type), INTENT(INOUT)            :: transport_env
     161              : 
     162              :       CHARACTER(len=*), PARAMETER :: routineN = 'transport_init_read_input'
     163              : 
     164              :       INTEGER                                            :: contact_bandwidth, contact_injsign, &
     165              :                                                             contact_natoms, contact_start, handle, &
     166              :                                                             i, n_contacts, stride_contacts
     167            0 :       INTEGER, DIMENSION(:), POINTER                     :: i_vals
     168              :       LOGICAL                                            :: contact_explicit, injecting_contact, &
     169              :                                                             obc_equilibrium, one_circle
     170              :       TYPE(section_vals_type), POINTER                   :: beyn_section, contact_section, &
     171              :                                                             pexsi_section, transport_section
     172              : 
     173            0 :       CALL timeset(routineN, handle)
     174              : 
     175            0 :       transport_section => section_vals_get_subs_vals(input, "DFT%TRANSPORT")
     176            0 :       contact_section => section_vals_get_subs_vals(transport_section, "CONTACT")
     177            0 :       beyn_section => section_vals_get_subs_vals(transport_section, "BEYN")
     178            0 :       pexsi_section => section_vals_get_subs_vals(transport_section, "PEXSI")
     179            0 :       CALL section_vals_get(contact_section, explicit=contact_explicit, n_repetition=n_contacts)
     180              : 
     181            0 :       NULLIFY (i_vals)
     182              : ! read from input
     183            0 :       CALL section_vals_val_get(transport_section, "TRANSPORT_METHOD", i_val=transport_env%params%method)
     184            0 :       CALL section_vals_val_get(transport_section, "INJECTION_METHOD", i_val=transport_env%params%injection_method)
     185              :       CALL section_vals_val_get(transport_section, "REAL_AXIS_INTEGRATION_METHOD", &
     186            0 :                                 i_val=transport_env%params%rlaxis_integration_method)
     187            0 :       CALL section_vals_val_get(transport_section, "QT_FORMALISM", i_val=transport_env%params%qt_formalism)
     188            0 :       CALL section_vals_val_get(transport_section, "LINEAR_SOLVER", i_val=transport_env%params%linear_solver)
     189              :       CALL section_vals_val_get(transport_section, "MATRIX_INVERSION_METHOD", &
     190            0 :                                 i_val=transport_env%params%matrixinv_method)
     191            0 :       CALL section_vals_val_get(transport_section, "CONTACT_FILLING", i_val=transport_env%params%transport_neutral)
     192            0 :       CALL section_vals_val_get(transport_section, "N_KPOINTS", i_val=transport_env%params%n_kpoint)
     193            0 :       CALL section_vals_val_get(transport_section, "NUM_INTERVAL", i_val=transport_env%params%num_interval)
     194              :       CALL section_vals_val_get(transport_section, "TASKS_PER_ENERGY_POINT", &
     195            0 :                                 i_val=transport_env%params%tasks_per_energy_point)
     196            0 :       CALL section_vals_val_get(transport_section, "TASKS_PER_POLE", i_val=transport_env%params%tasks_per_pole)
     197            0 :       CALL section_vals_val_get(transport_section, "NUM_POLE", i_val=transport_env%params%num_pole)
     198            0 :       CALL section_vals_val_get(transport_section, "GPUS_PER_POINT", i_val=transport_env%params%gpus_per_point)
     199            0 :       CALL section_vals_val_get(transport_section, "N_POINTS_INV", i_val=transport_env%params%n_points_inv)
     200            0 :       CALL section_vals_val_get(transport_section, "COLZERO_THRESHOLD", r_val=transport_env%params%colzero_threshold)
     201            0 :       CALL section_vals_val_get(transport_section, "EPS_LIMIT", r_val=transport_env%params%eps_limit)
     202            0 :       CALL section_vals_val_get(transport_section, "EPS_LIMIT_CC", r_val=transport_env%params%eps_limit_cc)
     203            0 :       CALL section_vals_val_get(transport_section, "EPS_DECAY", r_val=transport_env%params%eps_decay)
     204              :       CALL section_vals_val_get(transport_section, "EPS_SINGULARITY_CURVATURES", &
     205            0 :                                 r_val=transport_env%params%eps_singularity_curvatures)
     206            0 :       CALL section_vals_val_get(transport_section, "EPS_MU", r_val=transport_env%params%eps_mu)
     207            0 :       CALL section_vals_val_get(transport_section, "EPS_EIGVAL_DEGEN", r_val=transport_env%params%eps_eigval_degen)
     208            0 :       CALL section_vals_val_get(transport_section, "EPS_FERMI", r_val=transport_env%params%eps_fermi)
     209            0 :       CALL section_vals_val_get(transport_section, "ENERGY_INTERVAL", r_val=transport_env%params%energy_interval)
     210            0 :       CALL section_vals_val_get(transport_section, "MIN_INTERVAL", r_val=transport_env%params%min_interval)
     211            0 :       CALL section_vals_val_get(transport_section, "TEMPERATURE", r_val=transport_env%params%temperature)
     212            0 :       CALL section_vals_val_get(transport_section, "DENSITY_MIXING", r_val=transport_env%params%dens_mixing)
     213            0 :       CALL section_vals_val_get(transport_section, "CSR_SCREENING", l_val=transport_env%csr_screening)
     214              : 
     215              :       ! logical*1 to logical*4 , l_val is logical*1 and c_bool is equivalent to logical*4
     216            0 :       CALL section_vals_val_get(transport_section, "OBC_EQUILIBRIUM", l_val=obc_equilibrium)
     217            0 :       IF (obc_equilibrium) THEN
     218            0 :          transport_env%params%obc_equilibrium = .TRUE.
     219              :       ELSE
     220            0 :          transport_env%params%obc_equilibrium = .FALSE.
     221              :       END IF
     222              : 
     223            0 :       CALL section_vals_val_get(transport_section, "CUTOUT", i_vals=i_vals)
     224            0 :       transport_env%params%cutout = i_vals
     225              : 
     226              :       CALL section_vals_val_get(beyn_section, "TASKS_PER_INTEGRATION_POINT", &
     227            0 :                                 i_val=transport_env%params%tasks_per_integration_point)
     228            0 :       CALL section_vals_val_get(beyn_section, "N_POINTS_BEYN", i_val=transport_env%params%n_points_beyn)
     229            0 :       CALL section_vals_val_get(beyn_section, "N_RAND", r_val=transport_env%params%n_rand_beyn)
     230            0 :       CALL section_vals_val_get(beyn_section, "N_RAND_CC", r_val=transport_env%params%n_rand_cc_beyn)
     231            0 :       CALL section_vals_val_get(beyn_section, "SVD_CUTOFF", r_val=transport_env%params%svd_cutoff)
     232            0 :       CALL section_vals_val_get(beyn_section, "ONE_CIRCLE", l_val=one_circle)
     233            0 :       IF (one_circle) THEN
     234            0 :          transport_env%params%ncrc_beyn = 1
     235              :       ELSE
     236            0 :          transport_env%params%ncrc_beyn = 2
     237              :       END IF
     238              : 
     239            0 :       CALL section_vals_val_get(pexsi_section, "ORDERING", i_val=transport_env%params%ordering)
     240            0 :       CALL section_vals_val_get(pexsi_section, "ROW_ORDERING", i_val=transport_env%params%row_ordering)
     241            0 :       CALL section_vals_val_get(pexsi_section, "VERBOSITY", i_val=transport_env%params%verbosity)
     242            0 :       CALL section_vals_val_get(pexsi_section, "NP_SYMB_FACT", i_val=transport_env%params%pexsi_np_symb_fact)
     243              : 
     244            0 :       IF (contact_explicit) THEN
     245            0 :          transport_env%params%num_contacts = n_contacts
     246            0 :          stride_contacts = 5
     247            0 :          transport_env%params%stride_contacts = stride_contacts
     248            0 :          ALLOCATE (transport_env%contacts_data(stride_contacts*n_contacts))
     249              : 
     250            0 :          DO i = 1, n_contacts
     251            0 :             CALL section_vals_val_get(contact_section, "BANDWIDTH", i_rep_section=i, i_val=contact_bandwidth)
     252            0 :             CALL section_vals_val_get(contact_section, "START", i_rep_section=i, i_val=contact_start)
     253            0 :             CALL section_vals_val_get(contact_section, "N_ATOMS", i_rep_section=i, i_val=contact_natoms)
     254            0 :             CALL section_vals_val_get(contact_section, "INJECTION_SIGN", i_rep_section=i, i_val=contact_injsign)
     255            0 :             CALL section_vals_val_get(contact_section, "INJECTING_CONTACT", i_rep_section=i, l_val=injecting_contact)
     256              : 
     257            0 :             IF (contact_natoms <= 0) CPABORT("Number of atoms in contact region needs to be defined.")
     258              : 
     259            0 :             transport_env%contacts_data((i - 1)*stride_contacts + 1) = contact_bandwidth
     260            0 :             transport_env%contacts_data((i - 1)*stride_contacts + 2) = contact_start - 1 ! C indexing
     261            0 :             transport_env%contacts_data((i - 1)*stride_contacts + 3) = contact_natoms
     262            0 :             transport_env%contacts_data((i - 1)*stride_contacts + 4) = contact_injsign
     263              : 
     264            0 :             IF (injecting_contact) THEN
     265            0 :                transport_env%contacts_data((i - 1)*stride_contacts + 5) = 1
     266              :             ELSE
     267            0 :                transport_env%contacts_data((i - 1)*stride_contacts + 5) = 0
     268              :             END IF
     269              :          END DO
     270            0 :          transport_env%params%contacts_data = C_LOC(transport_env%contacts_data(1))
     271              :       ELSE
     272            0 :          CPABORT("No contact region is defined.")
     273              :       END IF
     274              : 
     275            0 :       CALL timestop(handle)
     276              : 
     277            0 :    END SUBROUTINE transport_init_read_input
     278              : 
     279              : ! **************************************************************************************************
     280              : !> \brief initializes the transport environment
     281              : !> \param ks_env ...
     282              : !> \param[inout] transport_env the transport env to be initialized
     283              : !> \param[in]    template_matrix   template matrix to keep the sparsity of matrices fixed
     284              : !> \author Mohammad Hossein Bani-Hashemian
     285              : ! **************************************************************************************************
     286            0 :    SUBROUTINE transport_initialize(ks_env, transport_env, template_matrix)
     287              :       TYPE(qs_ks_env_type), POINTER                      :: ks_env
     288              :       TYPE(transport_env_type), INTENT(INOUT)            :: transport_env
     289              :       TYPE(dbcsr_type), INTENT(IN)                       :: template_matrix
     290              : 
     291              :       CHARACTER(len=*), PARAMETER :: routineN = 'transport_initialize'
     292              : 
     293              :       INTEGER                                            :: handle, numnodes, unit_nr
     294              :       TYPE(cp_logger_type), POINTER                      :: logger
     295              : 
     296            0 :       CALL timeset(routineN, handle)
     297              : 
     298            0 :       CALL cite_reference(Bruck2014)
     299              : 
     300            0 :       logger => cp_get_default_logger()
     301            0 :       IF (logger%para_env%is_source()) THEN
     302            0 :          unit_nr = cp_logger_get_default_unit_nr(logger, local=.TRUE.)
     303              :       ELSE
     304            0 :          unit_nr = -1
     305              :       END IF
     306              : 
     307            0 :       numnodes = logger%para_env%num_pe
     308              : 
     309            0 :       IF (dbcsr_has_symmetry(template_matrix)) THEN
     310            0 :          CALL dbcsr_copy(transport_env%template_matrix_sym, template_matrix)
     311            0 :          CALL dbcsr_desymmetrize(transport_env%template_matrix_sym, transport_env%template_matrix_nosym)
     312              :       ELSE
     313            0 :          CALL dbcsr_copy(transport_env%template_matrix_nosym, template_matrix)
     314            0 :          CALL dbcsr_copy(transport_env%template_matrix_sym, template_matrix)
     315              :       END IF
     316              : 
     317            0 :       ALLOCATE (transport_env%dm_imag)
     318              :       CALL dbcsr_create(transport_env%dm_imag, "imaginary DM", &
     319            0 :                         template=template_matrix, matrix_type=dbcsr_type_no_symmetry)
     320            0 :       CALL dbcsr_set(transport_env%dm_imag, 0.0_dp)
     321              : 
     322              :       CALL dbcsr_create(transport_env%csr_sparsity, "CSR sparsity", &
     323            0 :                         template=transport_env%template_matrix_sym)
     324            0 :       CALL dbcsr_copy(transport_env%csr_sparsity, transport_env%template_matrix_sym)
     325              : 
     326            0 :       CALL cp_dbcsr_to_csr_screening(ks_env, transport_env%csr_sparsity)
     327              : 
     328            0 :       IF (.NOT. transport_env%csr_screening) CALL dbcsr_set(transport_env%csr_sparsity, 1.0_dp)
     329              :       CALL dbcsr_csr_create_from_dbcsr(transport_env%template_matrix_nosym, &
     330              :                                        transport_env%s_matrix, &
     331              :                                        dbcsr_csr_dbcsr_blkrow_dist, &
     332              :                                        csr_sparsity=transport_env%csr_sparsity, &
     333            0 :                                        numnodes=numnodes)
     334              : 
     335            0 :       CALL dbcsr_csr_print_sparsity(transport_env%s_matrix, unit_nr)
     336              : 
     337            0 :       CALL dbcsr_convert_dbcsr_to_csr(transport_env%template_matrix_nosym, transport_env%s_matrix)
     338              : 
     339            0 :       CALL dbcsr_csr_create(transport_env%ks_matrix, transport_env%s_matrix)
     340            0 :       CALL dbcsr_csr_create(transport_env%p_matrix, transport_env%s_matrix)
     341            0 :       CALL dbcsr_csr_create(transport_env%imagp_matrix, transport_env%s_matrix)
     342              : 
     343            0 :       CALL timestop(handle)
     344              : 
     345            0 :    END SUBROUTINE transport_initialize
     346              : 
     347              : ! **************************************************************************************************
     348              : !> \brief SCF calcualtion with an externally evaluated density matrix
     349              : !> \param[inout] transport_env  transport environment
     350              : !> \param[in]    matrix_s       DBCSR overlap matrix
     351              : !> \param[in]    matrix_ks      DBCSR Kohn-Sham matrix
     352              : !> \param[inout] matrix_p       DBCSR density matrix
     353              : !> \param[in]    nelectron_spin number of electrons
     354              : !> \param[in]    natoms         number of atoms
     355              : !> \param[in]    energy_diff    scf energy difference
     356              : !> \param[in]    iscf           the current scf iteration
     357              : !> \param[in]    extra_scf      whether or not an extra scf step will be performed
     358              : !> \par History
     359              : !>       12.2012 created [Hossein Bani-Hashemian]
     360              : !>       12.2014 revised [Hossein Bani-Hashemian]
     361              : !> \author Mohammad Hossein Bani-Hashemian
     362              : ! **************************************************************************************************
     363            0 :    SUBROUTINE external_scf_method(transport_env, matrix_s, matrix_ks, matrix_p, &
     364              :                                   nelectron_spin, natoms, energy_diff, iscf, extra_scf)
     365              : 
     366              :       TYPE(transport_env_type), INTENT(INOUT)            :: transport_env
     367              :       TYPE(dbcsr_type), INTENT(IN)                       :: matrix_s, matrix_ks
     368              :       TYPE(dbcsr_type), INTENT(INOUT)                    :: matrix_p
     369              :       INTEGER, INTENT(IN)                                :: nelectron_spin, natoms
     370              :       REAL(dp), INTENT(IN)                               :: energy_diff
     371              :       INTEGER, INTENT(IN)                                :: iscf
     372              :       LOGICAL, INTENT(IN)                                :: extra_scf
     373              : 
     374              :       CHARACTER(len=*), PARAMETER :: routineN = 'external_scf_method'
     375              : 
     376              :       TYPE(cp2k_csr_interop_type)                        :: imagp_mat, ks_mat, p_mat, s_mat
     377              : 
     378              :       PROCEDURE(c_scf_routine), POINTER        :: c_method
     379              :       INTEGER                                  :: handle
     380              : 
     381            0 :       CALL timeset(routineN, handle)
     382              : 
     383            0 :       CALL C_F_PROCPOINTER(transport_env%ext_c_method_ptr, c_method)
     384            0 :       IF (.NOT. C_ASSOCIATED(transport_env%ext_c_method_ptr)) &
     385              :          CALL cp_abort(__LOCATION__, &
     386              :                        "MISSING C/C++ ROUTINE: The TRANSPORT section is meant to be used together with an external "// &
     387            0 :                        "program, e.g. the quantum transport code OMEN, that provides CP2K with a density matrix.")
     388              : 
     389            0 :       transport_env%params%n_occ = nelectron_spin
     390            0 :       transport_env%params%n_atoms = natoms
     391            0 :       transport_env%params%energy_diff = energy_diff
     392            0 :       transport_env%params%evoltfactor = evolt
     393            0 :       transport_env%params%e_charge = e_charge
     394            0 :       transport_env%params%boltzmann = boltzmann
     395            0 :       transport_env%params%h_bar = h_bar
     396            0 :       transport_env%params%iscf = iscf
     397            0 :       transport_env%params%extra_scf = LOGICAL(extra_scf, C_BOOL) ! C_BOOL and Fortran logical don't have the same size
     398              : 
     399            0 :       CALL csr_interop_nullify(s_mat)
     400            0 :       CALL csr_interop_nullify(ks_mat)
     401            0 :       CALL csr_interop_nullify(p_mat)
     402            0 :       CALL csr_interop_nullify(imagp_mat)
     403              : 
     404            0 :       CALL dbcsr_copy(transport_env%template_matrix_sym, matrix_s, keep_sparsity=.TRUE.)
     405            0 :       CALL convert_dbcsr_to_csr_interop(transport_env%template_matrix_sym, transport_env%s_matrix, s_mat)
     406              : 
     407            0 :       CALL dbcsr_copy(transport_env%template_matrix_sym, matrix_ks, keep_sparsity=.TRUE.)
     408            0 :       CALL convert_dbcsr_to_csr_interop(transport_env%template_matrix_sym, transport_env%ks_matrix, ks_mat)
     409              : 
     410            0 :       CALL dbcsr_copy(transport_env%template_matrix_sym, matrix_p, keep_sparsity=.TRUE.)
     411            0 :       CALL convert_dbcsr_to_csr_interop(transport_env%template_matrix_sym, transport_env%p_matrix, p_mat)
     412              : 
     413            0 :       CALL dbcsr_copy(transport_env%template_matrix_sym, matrix_s, keep_sparsity=.TRUE.)
     414            0 :       CALL convert_dbcsr_to_csr_interop(transport_env%template_matrix_sym, transport_env%imagp_matrix, imagp_mat)
     415              : 
     416            0 :       CALL c_method(transport_env%params, s_mat, ks_mat, p_mat, imagp_mat)
     417              : 
     418            0 :       CALL convert_csr_interop_to_dbcsr(p_mat, transport_env%p_matrix, transport_env%template_matrix_nosym)
     419            0 :       CALL dbcsr_copy(matrix_p, transport_env%template_matrix_nosym)
     420              : 
     421            0 :       CALL convert_csr_interop_to_dbcsr(imagp_mat, transport_env%imagp_matrix, transport_env%template_matrix_nosym)
     422            0 :       CALL dbcsr_copy(transport_env%dm_imag, transport_env%template_matrix_nosym)
     423              : 
     424            0 :       CALL timestop(handle)
     425              : 
     426            0 :    END SUBROUTINE external_scf_method
     427              : 
     428              : ! **************************************************************************************************
     429              : !> \brief converts a DBCSR matrix to a C-interoperable CSR matrix
     430              : !> \param[in]    dbcsr_mat  DBCSR matrix to be converted
     431              : !> \param[inout] csr_mat    auxiliary CSR matrix
     432              : !> \param[inout] csr_interop_mat C-interoperable CSR matrix
     433              : !> \author Mohammad Hossein Bani-Hashemian
     434              : ! **************************************************************************************************
     435            0 :    SUBROUTINE convert_dbcsr_to_csr_interop(dbcsr_mat, csr_mat, csr_interop_mat)
     436              : 
     437              :       TYPE(dbcsr_type), INTENT(IN)          :: dbcsr_mat
     438              :       TYPE(dbcsr_csr_type), INTENT(INOUT)            :: csr_mat
     439              :       TYPE(cp2k_csr_interop_type), INTENT(INOUT)      :: csr_interop_mat
     440              : 
     441              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'convert_dbcsr_to_csr_interop'
     442              : 
     443              :       INTEGER                                  :: handle
     444            0 :       INTEGER, ALLOCATABLE, DIMENSION(:)       :: nrows_local_all, first_row_all
     445            0 :       INTEGER(C_INT), DIMENSION(:), POINTER    :: colind_local, rowptr_local, nzerow_local
     446            0 :       REAL(C_DOUBLE), DIMENSION(:), POINTER    :: nzvals_local
     447              :       TYPE(cp_logger_type), POINTER            :: logger
     448              : 
     449            0 :       CALL timeset(routineN, handle)
     450              : 
     451            0 :       logger => cp_get_default_logger()
     452              : 
     453              : ! dbcsr to csr
     454            0 :       CALL dbcsr_convert_dbcsr_to_csr(dbcsr_mat, csr_mat)
     455              : 
     456              : ! csr to csr_interop
     457            0 :       rowptr_local => csr_mat%rowptr_local
     458            0 :       colind_local => csr_mat%colind_local
     459            0 :       nzerow_local => csr_mat%nzerow_local
     460            0 :       nzvals_local => csr_mat%nzval_local%r_dp ! support real double percision for now
     461              : 
     462            0 :       IF (SIZE(rowptr_local) == 0) THEN
     463            0 :          csr_interop_mat%rowptr_local = C_NULL_PTR
     464              :       ELSE
     465            0 :          csr_interop_mat%rowptr_local = C_LOC(rowptr_local(1))
     466              :       END IF
     467              : 
     468            0 :       IF (SIZE(colind_local) == 0) THEN
     469            0 :          csr_interop_mat%colind_local = C_NULL_PTR
     470              :       ELSE
     471            0 :          csr_interop_mat%colind_local = C_LOC(colind_local(1))
     472              :       END IF
     473              : 
     474            0 :       IF (SIZE(nzerow_local) == 0) THEN
     475            0 :          csr_interop_mat%nzerow_local = C_NULL_PTR
     476              :       ELSE
     477            0 :          csr_interop_mat%nzerow_local = C_LOC(nzerow_local(1))
     478              :       END IF
     479              : 
     480            0 :       IF (SIZE(nzvals_local) == 0) THEN
     481            0 :          csr_interop_mat%nzvals_local = C_NULL_PTR
     482              :       ELSE
     483            0 :          csr_interop_mat%nzvals_local = C_LOC(nzvals_local(1))
     484              :       END IF
     485              : 
     486              :       ASSOCIATE (mp_group => logger%para_env, mepos => logger%para_env%mepos, num_pe => logger%para_env%num_pe)
     487            0 :          ALLOCATE (nrows_local_all(0:num_pe - 1), first_row_all(0:num_pe - 1))
     488            0 :          CALL mp_group%allgather(csr_mat%nrows_local, nrows_local_all)
     489            0 :          CALL cumsum_i(nrows_local_all, first_row_all)
     490              : 
     491            0 :          IF (mepos == 0) THEN
     492            0 :             csr_interop_mat%first_row = 0
     493              :          ELSE
     494            0 :             csr_interop_mat%first_row = first_row_all(mepos - 1)
     495              :          END IF
     496              :       END ASSOCIATE
     497            0 :       csr_interop_mat%nrows_total = csr_mat%nrows_total
     498            0 :       csr_interop_mat%ncols_total = csr_mat%ncols_total
     499            0 :       csr_interop_mat%nze_local = csr_mat%nze_local
     500            0 :       IF (csr_mat%nze_total > HUGE(csr_interop_mat%nze_total)) THEN
     501            0 :          CPABORT("overflow in nze")
     502              :       END IF
     503            0 :       csr_interop_mat%nze_total = INT(csr_mat%nze_total, KIND=KIND(csr_interop_mat%nze_total))
     504            0 :       csr_interop_mat%nrows_local = csr_mat%nrows_local
     505            0 :       csr_interop_mat%data_type = csr_mat%nzval_local%data_type
     506              : 
     507            0 :       CALL timestop(handle)
     508              : 
     509              :    CONTAINS
     510              : ! **************************************************************************************************
     511              : !> \brief cumulative sum of a 1d array of integers
     512              : !> \param[in]  arr    input array
     513              : !> \param[out] cumsum cumulative sum of the input array
     514              : ! **************************************************************************************************
     515            0 :       SUBROUTINE cumsum_i(arr, cumsum)
     516              :       INTEGER, DIMENSION(:), INTENT(IN)                  :: arr
     517              :       INTEGER, DIMENSION(SIZE(arr)), INTENT(OUT)         :: cumsum
     518              : 
     519              :       INTEGER                                            :: i
     520              : 
     521            0 :          cumsum(1) = arr(1)
     522            0 :          DO i = 2, SIZE(arr)
     523            0 :             cumsum(i) = cumsum(i - 1) + arr(i)
     524              :          END DO
     525            0 :       END SUBROUTINE cumsum_i
     526              : 
     527              :    END SUBROUTINE convert_dbcsr_to_csr_interop
     528              : 
     529              : ! **************************************************************************************************
     530              : !> \brief converts a C-interoperable CSR matrix to a DBCSR matrix
     531              : !> \param[in] csr_interop_mat C-interoperable CSR matrix
     532              : !> \param[inout] csr_mat         auxiliary CSR matrix
     533              : !> \param[inout] dbcsr_mat       DBCSR matrix
     534              : !> \author Mohammad Hossein Bani-Hashemian
     535              : ! **************************************************************************************************
     536            0 :    SUBROUTINE convert_csr_interop_to_dbcsr(csr_interop_mat, csr_mat, dbcsr_mat)
     537              : 
     538              :       TYPE(cp2k_csr_interop_type), INTENT(IN)            :: csr_interop_mat
     539              :       TYPE(dbcsr_csr_type), INTENT(INOUT)                      :: csr_mat
     540              :       TYPE(dbcsr_type), INTENT(INOUT)                    :: dbcsr_mat
     541              : 
     542              :       CHARACTER(LEN=*), PARAMETER :: routineN = 'convert_csr_interop_to_dbcsr'
     543              : 
     544              :       INTEGER                                            :: data_type, handle, ncols_total, &
     545              :                                                             nrows_local, nrows_total, nze_local, &
     546              :                                                             nze_total
     547            0 :       INTEGER, DIMENSION(:), POINTER                     :: colind_local, nzerow_local, rowptr_local
     548            0 :       REAL(dp), DIMENSION(:), POINTER                    :: nzvals_local
     549              : 
     550            0 :       CALL timeset(routineN, handle)
     551              : 
     552              : ! csr_interop to csr
     553              :       CALL csr_interop_matrix_get_info(csr_interop_mat, &
     554              :                                        nrows_total=nrows_total, ncols_total=ncols_total, nze_local=nze_local, &
     555              :                                        nze_total=nze_total, nrows_local=nrows_local, data_type=data_type, &
     556              :                                        rowptr_local=rowptr_local, colind_local=colind_local, &
     557            0 :                                        nzerow_local=nzerow_local, nzvals_local=nzvals_local)
     558              : 
     559            0 :       csr_mat%nrows_total = nrows_total
     560            0 :       csr_mat%ncols_total = ncols_total
     561            0 :       csr_mat%nze_local = nze_local
     562            0 :       csr_mat%nze_total = nze_total
     563            0 :       csr_mat%nrows_local = nrows_local
     564            0 :       csr_mat%nzval_local%data_type = data_type
     565              : 
     566            0 :       csr_mat%rowptr_local = rowptr_local
     567            0 :       csr_mat%colind_local = colind_local
     568            0 :       csr_mat%nzerow_local = nzerow_local
     569            0 :       csr_mat%nzval_local%r_dp = nzvals_local
     570              : 
     571              : ! csr to dbcsr
     572            0 :       CALL dbcsr_convert_csr_to_dbcsr(dbcsr_mat, csr_mat)
     573              : 
     574            0 :       CALL timestop(handle)
     575              : 
     576            0 :    END SUBROUTINE convert_csr_interop_to_dbcsr
     577              : 
     578              : ! **************************************************************************************************
     579              : !> \brief extraxts zeff (effective nuclear charges per atom) and nsgf (the size
     580              : !>   of spherical Gaussian basis functions per atom) from qs_env and initializes
     581              : !>   the corresponding arrays in transport_env%params
     582              : !> \param[in] qs_env qs environment
     583              : !> \param[inout] transport_env transport environment
     584              : !> \author Mohammad Hossein Bani-Hashemian
     585              : ! **************************************************************************************************
     586            0 :    SUBROUTINE transport_set_contact_params(qs_env, transport_env)
     587              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     588              :       TYPE(transport_env_type), INTENT(INOUT)            :: transport_env
     589              : 
     590              :       INTEGER                                            :: i, iat, ikind, natom, nkind
     591            0 :       INTEGER, DIMENSION(:), POINTER                     :: atom_list
     592              :       REAL(KIND=dp)                                      :: zeff
     593            0 :       TYPE(atomic_kind_type), DIMENSION(:), POINTER      :: atomic_kind_set
     594              :       TYPE(atomic_kind_type), POINTER                    :: atomic_kind
     595            0 :       TYPE(particle_type), DIMENSION(:), POINTER         :: particle_set
     596            0 :       TYPE(qs_kind_type), DIMENSION(:), POINTER          :: qs_kind_set
     597              : 
     598            0 :       CALL get_qs_env(qs_env, nkind=nkind, natom=natom)
     599              :       CALL get_qs_env(qs_env, particle_set=particle_set, &
     600              :                       qs_kind_set=qs_kind_set, &
     601            0 :                       atomic_kind_set=atomic_kind_set)
     602              : 
     603            0 :       ALLOCATE (transport_env%nsgf(natom))
     604            0 :       ALLOCATE (transport_env%zeff(natom))
     605            0 :       CALL get_particle_set(particle_set, qs_kind_set, nsgf=transport_env%nsgf)
     606              : 
     607              :       ! reference charges
     608            0 :       DO ikind = 1, nkind
     609            0 :          CALL get_qs_kind(qs_kind_set(ikind), zeff=zeff)
     610            0 :          atomic_kind => atomic_kind_set(ikind)
     611            0 :          CALL get_atomic_kind(atomic_kind, atom_list=atom_list)
     612            0 :          DO iat = 1, SIZE(atom_list)
     613            0 :             i = atom_list(iat)
     614            0 :             transport_env%zeff(i) = zeff
     615              :          END DO
     616              :       END DO
     617              : 
     618            0 :       IF (natom == 0) THEN
     619            0 :          transport_env%params%nsgf = C_NULL_PTR
     620            0 :          transport_env%params%zeff = C_NULL_PTR
     621              :       ELSE
     622            0 :          transport_env%params%nsgf = C_LOC(transport_env%nsgf(1))
     623            0 :          transport_env%params%zeff = C_LOC(transport_env%zeff(1))
     624              :       END IF
     625              : 
     626            0 :    END SUBROUTINE transport_set_contact_params
     627              : 
     628              : !**************************************************************************************************
     629              : !> \brief evaluates current density using the imaginary part of the density matrix and prints it
     630              : !>        into cube files
     631              : !> \param[in] input the input
     632              : !> \param[in] qs_env qs environment
     633              : !> \author Mohammad Hossein Bani-Hashemian
     634              : ! **************************************************************************************************
     635        12775 :    SUBROUTINE transport_current(input, qs_env)
     636              :       TYPE(section_vals_type), POINTER                   :: input
     637              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     638              : 
     639              :       CHARACTER(len=*), PARAMETER                        :: routineN = 'transport_current'
     640              : 
     641              :       CHARACTER(len=14)                                  :: ext
     642              :       CHARACTER(len=2)                                   :: sdir
     643              :       INTEGER                                            :: dir, handle, unit_nr
     644              :       LOGICAL                                            :: do_current_cube, do_transport, mpi_io
     645              :       TYPE(cp_logger_type), POINTER                      :: logger
     646              :       TYPE(current_env_type)                             :: current_env
     647              :       TYPE(dbcsr_type), POINTER                          :: zero
     648              :       TYPE(dft_control_type), POINTER                    :: dft_control
     649              :       TYPE(particle_list_type), POINTER                  :: particles
     650              :       TYPE(pw_c1d_gs_type)                               :: gs
     651        12775 :       TYPE(pw_c1d_gs_type), DIMENSION(:), POINTER        :: rho_g
     652              :       TYPE(pw_env_type), POINTER                         :: pw_env
     653              :       TYPE(pw_pool_type), POINTER                        :: auxbas_pw_pool
     654              :       TYPE(pw_r3d_rs_type)                               :: rs
     655        12775 :       TYPE(pw_r3d_rs_type), DIMENSION(:), POINTER        :: rho_r
     656              :       TYPE(qs_rho_type), POINTER                         :: rho
     657              :       TYPE(qs_subsys_type), POINTER                      :: subsys
     658              :       TYPE(section_vals_type), POINTER                   :: dft_section
     659              :       TYPE(transport_env_type), POINTER                  :: transport_env
     660              : 
     661        12775 :       CALL timeset(routineN, handle)
     662              : 
     663        12775 :       logger => cp_get_default_logger()
     664        12775 :       dft_section => section_vals_get_subs_vals(input, "DFT")
     665              :       CALL get_qs_env(qs_env=qs_env, &
     666              :                       subsys=subsys, &
     667              :                       pw_env=pw_env, &
     668              :                       transport_env=transport_env, &
     669              :                       do_transport=do_transport, &
     670              :                       dft_control=dft_control, &
     671        12775 :                       rho=rho)
     672        12775 :       CALL qs_subsys_get(subsys, particles=particles)
     673        12775 :       CALL pw_env_get(pw_env, auxbas_pw_pool=auxbas_pw_pool)
     674        12775 :       CALL qs_rho_get(rho, rho_r=rho_r, rho_g=rho_g)
     675              : 
     676              :       do_current_cube = BTEST(cp_print_key_should_output(logger%iter_info, input, &
     677        12775 :                                                          "DFT%TRANSPORT%PRINT%CURRENT"), cp_p_file)
     678              : 
     679              :       ! check if transport is active i.e. the imaginary dm has been actually passed to cp2k by the external code
     680        12775 :       IF (do_transport) THEN
     681            0 :          IF (C_ASSOCIATED(transport_env%ext_c_method_ptr)) THEN
     682              : 
     683              :             ! calculate_jrho_resp uses sab_all which is not associated in DFTB environment
     684            0 :             IF (dft_control%qs_control%dftb) CPABORT("Current is not available for DFTB.")
     685            0 :             IF (dft_control%qs_control%xtb) CPABORT("Current is not available for xTB.")
     686              : 
     687              :             ! now do the current
     688            0 :             IF (do_current_cube) THEN
     689            0 :                current_env%gauge = -1
     690            0 :                current_env%gauge_init = .FALSE.
     691              : 
     692            0 :                CALL auxbas_pw_pool%create_pw(rs)
     693            0 :                CALL auxbas_pw_pool%create_pw(gs)
     694              : 
     695              :                NULLIFY (zero)
     696            0 :                ALLOCATE (zero)
     697            0 :                CALL dbcsr_create(zero, template=transport_env%dm_imag)
     698            0 :                CALL dbcsr_copy(zero, transport_env%dm_imag)
     699            0 :                CALL dbcsr_set(zero, 0.0_dp)
     700              : 
     701            0 :                DO dir = 1, 3
     702            0 :                   CALL pw_zero(rs)
     703            0 :                   CALL pw_zero(gs)
     704              :                   CALL calculate_jrho_resp(zero, transport_env%dm_imag, &
     705              :                                            zero, zero, dir, dir, rs, gs, qs_env, current_env, &
     706            0 :                                            retain_rsgrid=.TRUE.)
     707            0 :                   SELECT CASE (dir)
     708              :                   CASE (1)
     709            0 :                      sdir = "-x"
     710              :                   CASE (2)
     711            0 :                      sdir = "-y"
     712              :                   CASE (3)
     713            0 :                      sdir = "-z"
     714              :                   END SELECT
     715            0 :                   ext = sdir//".cube"
     716            0 :                   mpi_io = .TRUE.
     717              :                   unit_nr = cp_print_key_unit_nr(logger, dft_section, "TRANSPORT%PRINT%CURRENT", &
     718              :                                                  extension=ext, file_status="REPLACE", file_action="WRITE", &
     719            0 :                                                  log_filename=.FALSE., mpi_io=mpi_io)
     720              :                   CALL cp_pw_to_cube(rs, unit_nr, "Transport current", particles=particles, &
     721              :                                      stride=section_get_ivals(dft_section, "TRANSPORT%PRINT%CURRENT%STRIDE"), &
     722            0 :                                      mpi_io=mpi_io)
     723              :                   CALL cp_print_key_finished_output(unit_nr, logger, dft_section, "TRANSPORT%PRINT%CURRENT", &
     724            0 :                                                     mpi_io=mpi_io)
     725              :                END DO
     726              : 
     727            0 :                CALL dbcsr_deallocate_matrix(zero)
     728            0 :                CALL auxbas_pw_pool%give_back_pw(rs)
     729            0 :                CALL auxbas_pw_pool%give_back_pw(gs)
     730              :             END IF
     731              :          END IF
     732              : 
     733              :       END IF
     734              : 
     735        12775 :       CALL timestop(handle)
     736              : 
     737       932575 :    END SUBROUTINE transport_current
     738              : 
     739              : !**************************************************************************************************
     740              : !> \brief post scf calculations for transport
     741              : !> \param[in] qs_env qs environment
     742              : !> \author Mohammad Hossein Bani-Hashemian
     743              : ! **************************************************************************************************
     744        12775 :    SUBROUTINE qs_scf_post_transport(qs_env)
     745              :       TYPE(qs_environment_type), POINTER                 :: qs_env
     746              : 
     747              :       CHARACTER(len=*), PARAMETER :: routineN = 'qs_scf_post_transport'
     748              : 
     749              :       INTEGER                                            :: handle
     750              :       TYPE(section_vals_type), POINTER                   :: input
     751              : 
     752        12775 :       CALL timeset(routineN, handle)
     753              : 
     754        12775 :       NULLIFY (input)
     755              : 
     756              :       CALL get_qs_env(qs_env=qs_env, &
     757        12775 :                       input=input)
     758              : 
     759        12775 :       CALL transport_current(input, qs_env)
     760              : 
     761        12775 :       CALL timestop(handle)
     762              : 
     763        12775 :    END SUBROUTINE qs_scf_post_transport
     764              : 
     765              : END MODULE transport
        

Generated by: LCOV version 2.0-1