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

Generated by: LCOV version 2.0-1