!
! License-Identifier: GPL
!
! Copyright (C) 2006 The Yambo Team
!
! Authors (see AUTHORS file for details): AM DS AF
!
! headers
!
#include<dev_defs.h>
#include<y_memory.h>
!
subroutine K_kernel_default(iq,Xk,q,iHxc,i_block,i_Tgrp_k,i_Tgrp_p,H_pos_start,Co,mode_now,&
&                           l_matrix_init,l_write_kernel,l_tddft_gsum,l_tddft_rsum,l_std_alg,l_dir_alg,&
&                           win1_ID,win2_ID,tddft_wf)
 !
 ! Transitions loop of the BSE/TDDFT kernel for the block i_block: the matrix elements
 ! are computed one by one (exchange, TDDFT and correlation).
 ! Called by K_kernel.
 !

 use pars,          ONLY:SP,schlen,pi,cZERO,cI
 use LOGO,          ONLY:pickup_a_random
 use drivers,       ONLY:l_bs_fxc,l_tddft,l_rim_w
 use frequency,     ONLY:w_samp
 use electrons,     ONLY:levels,spin_occ,spin,n_spin,n_spinor,n_sp_pol
 use FFT_m,         ONLY:fft_size
 use gpu_m,         ONLY:have_gpu
 use interfaces,    ONLY:WF_load,WF_free,eval_G_minus_G,TDDFT_ALDA_eh_space_R_kernel
 use D_lattice,     ONLY:nsym,DL_vol,S_contains_TR,i_space_inv,sop_inv,sop_tab,&
&                        non_symmorphic_syms
 use R_lattice,     ONLY:qindx_X,bz_samp,rl_sop,&
&                        qindx_B,qindx_B_max,RIM_W,RIM_W_d,&
&                        RIM_ng,RIM_W_is_diagonal,RIM_W_ng
 use vec_operate,   ONLY:c2a
 use com,           ONLY:msg
 use stderr,        ONLY:intc
 use LIVE_t,        ONLY:live_timing
 use X_m,           ONLY:X_t
 use interfaces,    ONLY:el_density_and_current
 use QP_m,          ONLY:QP_ng_Vxc
 use BS_solvers,    ONLY:BSS_kernel_last_stored_block,load_Diago
 use BS,            ONLY:BS_bands,BS_res_K_corr,BS_W,BS_W_is_diagonal,BS_ID,&
&                        BS_res_K_exchange,BS_Block_size,l_BSE_kernel_full,BS_n_g_W,&
&                        O_ng,BS_n_g_exch,BS_n_g_fxc,BS_identifier,BS_LiveTiming_steps,&
&                        BS_K_dim,BS_K_is_ALDA,BS_K_coupling,BS_cpl_K_exchange,&
&                        BS_cpl_K_corr,K_EXCH_collision,K_CORR_collision,&
&                        WF_phase,n_BS_blks,BS_blk,BS_T_grp,BSK_IO_mode,BSK_IO_sum_value,&
&                        BS_nT_grps,BS_n_eh_spaces,BS_K_has_been_calculated_loaded,&
&                        l_BSE_minimize_memory,l_BSE_restart,l_BSE_kernel_complete,&
&                        BS_perturbative_SOC,BS_K_cutoff,BS_max_val,l_BS_magnons,&
&                        l_BS_ares_from_res
 use collision_el,  ONLY:elemental_collision_free
 use IO_int,        ONLY:io_control
 use IO_m,          ONLY:REP,OP_WR,RD,WR,WR_CL,OP_APP,io_BS_K,deliver_IO_error_message
 use TDDFT,         ONLY:FXC_K_diagonal,F_xc_gspace,FXC_n_g_corr,io_BS_Fxc, &
&                        FXC_mode,tddft_wf_t,l_Fxc_from_Vxc
 use xc_functionals,ONLY:V_xc,F_xc,F_xc_mat,DEV_VAR(F_xc_mat),magn,XC_potential_driver
 use global_XC,     ONLY:WF_xc_functional,WF_kind,WF_exx_fraction
 use wave_func,     ONLY:WF,rho_map,rho_map_thresh,rho_map_size
 use openmp,        ONLY:OPENMP_update,master_thread
 use timing_m,      ONLY:timing
 use parallel_m,    ONLY:myid,ncpu,PAR_K_scheme,PARALLEL_default_mode,&
 &                       master_cpu,mpi_comm_world
 use parallel_int,  ONLY:PP_redux_wait
 use MAGNONS,       ONLY:BSS_MAGN_free
 use PHOTOLUM,      ONLY:BSS_PL_free
 use parser_m,      ONLY:parser
 use mpi
 ! Using the following line instead of the general use mpi
 ! Gives compilation error with impi due to MPI_Get and MPI_Win_create
 !use mpi,           ONLY:MPI_INTEGER,MPI_ADDRESS_KIND,MPI_Win_create,MPI_Win_fence,MPI_Get,&
 !&                       MPI_Info_create,MPI_Info_set,MPI_info_free
 use y_memory_alloc
 implicit none

 !
 ! Input
 !
 integer,          intent(in)    :: iq,iHxc,i_block,i_Tgrp_k,i_Tgrp_p,H_pos_start(2)
 type(bz_samp),    intent(in)    :: Xk,q
 complex(SP),      intent(in)    :: Co
 character(1),     intent(in)    :: mode_now
 logical,          intent(in)    :: l_matrix_init,l_write_kernel,l_tddft_gsum,l_tddft_rsum,l_std_alg,l_dir_alg
 integer,          intent(in)    :: win1_ID,win2_ID
 type(tddft_wf_t), target, intent(inout) :: tddft_wf
 !
 ! Work Space (private in the OpenMP region)
 !
 real(SP) :: q_W_rot(3)
 integer ::     i_k_s,i_k_s_m1,i_p_s,i_k_bz,i_p_bz,i_k,i_p,i_kp_s,& 
&               i_kmq_s,i_kmq_s_m1,i_pmq_s,i_kmq_bz,i_pmq_bz,i_kmq,i_pmq,i_kp_mq_s,& 
&               i_Tk,i_Tp,H_pos(2),& 
&               i_v_k,i_v_p,i_c_k,i_c_p,i_k_sp_pol_c,i_p_sp_pol_c,i_k_sp_pol_v,i_p_sp_pol_v,iq_W,iq_W_bz,iq_W_s,ig_W,& 
&               i_kmq_t,i_pmq_t,i_k_bz_mem,i_kmq_bz_mem,qindx_tmp(2)
 integer ::     i_k_bz_last,i_p_bz_last,i_pmq_bz_last,i_kmq_bz_last, & 
&               iq_W_bz_last,ig_W_last,iq_W_bz_mq_last,ig_W_mq_last,ig_W_final
 integer ::     ig_kmq,ig_pmq,ig_W_mq,iq_W_bz_mq,iq_W_mq,iq_W_s_mq,is_k(4),os_k(4),is_p(4),os_p(4)
 !
 complex(SP) :: H_x,H_c,BS_mat_tmp
 complex(SP) :: sqrt_f_itk,sqrt_f_itp
 !
 complex(SP), allocatable DEV_ATTR :: O1(:),O2(:),O_times_W(:)
 !
 complex(SP),   external :: K_correlation_kernel_std,K_correlation_kernel_dir
 complex(SP),   external :: TDDFT_ALDA_eh_space_G_kernel,TDDFT_ALDA_eh_space_G_kernel_coupling
 complex(SP),   external :: K_exchange_kernel,K_exchange_kernel_coupling
 !
 ! MPI window
 !
 integer(kind=MPI_ADDRESS_KIND) :: get_index
 integer  :: mpi_err
 integer  :: remote_id
 !

   ! Indexes of the k and p points of the block
   i_k=BS_blk(i_block)%ik
   i_p=BS_blk(i_block)%ip
   !
   ! Table of the transitions and energies of the block
   call K_block_tables(i_block,i_Tgrp_k,i_Tgrp_p,H_pos_start,mode_now)
   !
   !DEV_OMP parallel default(shared), &
   !DEV_OMP private(i_Tk,i_Tp, O1,O2,O_times_W,H_pos,q_W_rot, &
   !DEV_OMP &       sqrt_f_itk,i_k_bz,i_v_k,i_c_k,i_k_sp_pol_c,i_k_sp_pol_v,i_k_s,is_k,os_k,i_k_s_m1,&
   !DEV_OMP &       sqrt_f_itp,i_p_bz,i_v_p,i_c_p,i_p_sp_pol_c,i_p_sp_pol_v,i_p_s,is_p,os_p,i_kp_s, &
   !DEV_OMP &       i_kmq_bz,ig_kmq,i_kmq,i_kmq_s,i_kmq_t,i_kmq_s_m1,i_kp_mq_s, &
   !DEV_OMP &       i_pmq_bz,ig_pmq,i_pmq,i_pmq_s,i_pmq_t,ig_W_final, &
   !DEV_OMP &       ig_W,iq_W,iq_W_s,iq_W_bz,iq_W_bz_mq,ig_W_mq,iq_W_mq,iq_W_s_mq,H_x,H_c, &
   !DEV_OMP &       i_p_bz_last,i_k_bz_last,i_pmq_bz_last,i_kmq_bz_last, &
   !DEV_OMP &       iq_W_bz_last,ig_W_last,iq_W_bz_mq_last,ig_W_mq_last, &
   !DEV_OMP &       i_k_bz_mem,i_kmq_bz_mem,qindx_tmp,BS_mat_tmp,tddft_wf )
   !
   call OPENMP_update(master_thread) 
   !
   ! Allocate tddft_wf variables neeeded within the OMP loop
   if (l_tddft_rsum.and.iHxc==2) then
     YAMBO_ALLOC(tddft_wf%WF_symm1,(rho_map_size,n_spinor))
     YAMBO_ALLOC(tddft_wf%WF_symm2,(rho_map_size,n_spinor))
     YAMBO_ALLOC(tddft_wf%rhotwr1,(rho_map_size*n_spinor*n_spinor))
     YAMBO_ALLOC(tddft_wf%rhotwr2,(rho_map_size*n_spinor*n_spinor))
   endif
   !
   i_p_bz_last=0   ; i_k_bz_last=0
   i_pmq_bz_last=0 ; i_kmq_bz_last=0
   !
   ! workspace (for thread-safety, these vars are kept as
   !            local in K_correlation_kernel*, and only allocated
   !            here for performance in the presence of GPUs)
   !
   if (iHxc==3) then
     YAMBO_ALLOC_GPU(O1,(BS_n_g_W))
     YAMBO_ALLOC_GPU(O2,(BS_n_g_W))
     YAMBO_ALLOC_GPU(O_times_W,(BS_n_g_W))
   endif
   !
   !DEV_OMP do collapse(2), schedule(dynamic)
   !
   do i_Tp=1,BS_T_grp(i_Tgrp_p)%size
     do i_Tk=1,BS_T_grp(i_Tgrp_k)%size
       !
       !
       if (BS_blk(i_block)%done(i_Tk,i_Tp)=="t".and.l_BSE_restart) cycle
       !
       ! H_pos defines the position inside the full bse matrix when
       ! l_BS_ares_from_res.  Otherwise it defines the position inside the R/C blocks
       !
       H_pos(1) = H_pos_start(1)+i_Tk
       H_pos(2) = H_pos_start(2)+i_Tp
       !
       ! When l_BS_ares_from_res=.false., the full coupling block, and
       ! the upper part of the anti-resonant blocks are also computed
       !
       ! When l_BSE_kernel_full the full blocks are computed
       !
       if (H_pos(1)>H_pos(2) .and. .not. l_BSE_kernel_full) cycle
       !
       i_k_bz  = BS_T_grp(i_Tgrp_k)%table(i_Tk,1) 
       i_p_bz  = BS_T_grp(i_Tgrp_p)%table(i_Tp,1) 
       !
       i_k_s =Xk%sstar(i_k_bz,2) 
       i_p_s =Xk%sstar(i_p_bz,2) 
       !
       i_k_s_m1= sop_inv(i_k_s) 
       i_kp_s  = sop_tab(i_k_s_m1,i_p_s) 
       !
       i_kmq_bz=qindx_X(iq,i_k_bz,1) 
       i_pmq_bz=qindx_X(iq,i_p_bz,1) 
       !
       i_kmq   =Xk%sstar(i_kmq_bz,1) 
       i_pmq   =Xk%sstar(i_pmq_bz,1) 
       !
       i_kmq_s =Xk%sstar(i_kmq_bz,2) 
       i_pmq_s =Xk%sstar(i_pmq_bz,2) 
       !
       if (iHxc==3) then 
         i_kmq_t=BS_blk(i_block)%kp_table(1,i_kmq) 
         i_pmq_t=BS_blk(i_block)%kp_table(2,i_pmq) 
       endif 
       !
       i_kmq_s_m1 = sop_inv(i_kmq_s) 
       i_kp_mq_s  = sop_tab(i_kmq_s_m1,i_pmq_s) 
       !
       if (iHxc==3) then 
         i_k_bz_mem=PAR_K_scheme%bz_index(i_k_bz) 
         if (i_p_bz_last/=i_p_bz.or.i_k_bz_last/=i_k_bz) then 
           i_p_bz_last=i_p_bz 
           i_k_bz_last=i_k_bz 
           if (i_k_bz_mem<0) then
             get_index = (-i_k_bz_mem-1)*Xk%nbz+i_p_bz-1
             remote_id=PAR_K_scheme%bz_id(i_k_bz)
             call MPI_Get(qindx_tmp(1), 1, MPI_INTEGER, remote_id, get_index, 1, MPI_INTEGER, win1_ID, mpi_err)
             call MPI_Get(qindx_tmp(2), 1, MPI_INTEGER, remote_id, get_index, 1, MPI_INTEGER, win2_ID, mpi_err)
             iq_W_bz=qindx_tmp(1) 
             ig_W   =qindx_tmp(2) 
           else 
             iq_W_bz=qindx_B(i_p_bz,i_k_bz_mem,1) 
             ig_W   =qindx_B(i_p_bz,i_k_bz_mem,2) 
           endif 
           iq_W_bz_last=iq_W_bz 
           ig_W_last   =ig_W    
         else 
           iq_W_bz=iq_W_bz_last 
           ig_W   =ig_W_last    
         endif 
         iq_W   =q%sstar( iq_W_bz ,1) 
         iq_W_s =q%sstar( iq_W_bz ,2) 
       endif 
       !
       i_v_k=BS_T_grp(i_Tgrp_k)%table(i_Tk,2) 
       i_c_k=BS_T_grp(i_Tgrp_k)%table(i_Tk,3) 
       !
       i_v_p=BS_T_grp(i_Tgrp_p)%table(i_Tp,2) 
       i_c_p=BS_T_grp(i_Tgrp_p)%table(i_Tp,3) 
       !
       i_k_sp_pol_c=BS_T_grp(i_Tgrp_k)%table(i_Tk,4) 
       i_p_sp_pol_c=BS_T_grp(i_Tgrp_p)%table(i_Tp,4) 
       i_k_sp_pol_v=BS_T_grp(i_Tgrp_k)%table(i_Tk,5) 
       i_p_sp_pol_v=BS_T_grp(i_Tgrp_p)%table(i_Tp,5) 
       !
       ig_kmq    = qindx_X(iq,i_k_bz,2) 
       ig_pmq    = qindx_X(iq,i_p_bz,2) 
       !        
       if (iHxc==3) then 
         i_kmq_bz_mem=PAR_K_scheme%bz_index(i_kmq_bz) 
         if (i_pmq_bz_last/=i_pmq_bz.or.i_kmq_bz_last/=i_kmq_bz) then 
           i_pmq_bz_last=i_pmq_bz 
           i_kmq_bz_last=i_kmq_bz 
           if (i_kmq_bz_mem<0) then 
             get_index = (-i_kmq_bz_mem-1)*Xk%nbz+i_pmq_bz-1
             remote_id=PAR_K_scheme%bz_id(i_kmq_bz)
             call MPI_Get(qindx_tmp(1), 1, MPI_INTEGER, remote_id, get_index, 1, MPI_INTEGER, win1_ID, mpi_err)
             call MPI_Get(qindx_tmp(2), 1, MPI_INTEGER, remote_id, get_index, 1, MPI_INTEGER, win2_ID, mpi_err)
             iq_W_bz_mq=qindx_tmp(1) 
             ig_W_mq   =qindx_tmp(2) 
           else 
             iq_W_bz_mq=qindx_B(i_pmq_bz,i_kmq_bz_mem,1) 
             ig_W_mq   =qindx_B(i_pmq_bz,i_kmq_bz_mem,2) 
           endif 
           iq_W_bz_mq_last=iq_W_bz_mq 
           ig_W_mq_last   =ig_W_mq    
         else 
           iq_W_bz_mq=iq_W_bz_mq_last 
           ig_W_mq   =ig_W_mq_last    
         endif
         !
         iq_W_mq   =q%sstar( iq_W_bz_mq,1) 
         iq_W_s_mq =q%sstar( iq_W_bz_mq,2) 
         !
         !if (iq==1) ig_W_final=      ig_W_mq
         !if (iq> 1) ig_W_final=G_m_G(ig_W_mq,g_rot(G_m_G(ig_pmq,ig_kmq),iq_W_s_mq))
         !
       endif 
       ! 
       if (iHxc==2.and.l_tddft_rsum) then 
         is_k = (/i_c_k,i_k,i_k_s,i_k_sp_pol_c/) 
         os_k = (/i_v_k,i_kmq,i_kmq_s,i_k_sp_pol_v/) 
         is_p = (/i_c_p,i_p,i_p_s,i_p_sp_pol_c/) 
         os_p = (/i_v_p,i_pmq,i_pmq_s,i_p_sp_pol_v/) 
       endif 
       !
       if (iHxc==3) then 
         !
         if (iq_W_bz/=iq_W_bz_mq.or.iq_W/=iq_W_mq.or.iq_W_s/=iq_W_s_mq) &
&           call error("Wrong transferred momentum")
         !
         !if ( G_m_G(ig_W,ig_W_mq) /= G_m_G(ig_kmq,ig_pmq) ) &
!&          call error("Wrong gvector shifts")
         !
         if ( (.not.BS_W_is_diagonal) .and. S_contains_TR(iq_W_s) &
         &                .and. i_space_inv == 0 ) iq_W=q%nibz+iq_W
         !
       endif
       !
       H_x=cZERO
       H_c=cZERO
       !
       select case (mode_now)
       !
       case ("R","F")
         !
         ! :::RESONANT/ANTI-RESONANT TERM:::
         !===================================
         !
         ! Exchange
         if (iHxc==1) H_x=K_exchange_kernel(iq, BS_n_g_exch,               &
&                                   BS_T_grp(i_Tgrp_p),i_Tp, BS_T_grp(i_Tgrp_k),i_Tk)
         !
         ! ALDA
         if (iHxc==2) then
           if(l_tddft_rsum)     H_x=H_x+TDDFT_ALDA_eh_space_R_kernel(is_k,os_k,is_p,os_p,tddft_wf,'RES')
           if(l_tddft_gsum)     H_x=H_x+TDDFT_ALDA_eh_space_G_kernel(BS_n_g_fxc,&
&                                   BS_T_grp(i_Tgrp_p),i_Tp, BS_T_grp(i_Tgrp_k),i_Tk)
         endif
         !
         ! Correlations
         if (iHxc==3) then
           ! q of the screened interaction in the BZ frame of the oscillators, R_W q_W (Cartesian)
           q_W_rot=matmul(rl_sop(:,:,iq_W_s),q%pt(iq_W,:))
           call c2a(v_in=q_W_rot,mode='ki2c')
           if (l_std_alg)       H_c=K_correlation_kernel_std(i_block,i_p,i_pmq, &
&                                   i_k_s,i_kp_s,i_c_k,i_c_p,i_kmq_s,i_kp_mq_s,i_v_k,i_v_p,     &
&                                   i_kmq_t,i_pmq_t,i_k_sp_pol_c,i_p_sp_pol_c,i_k_sp_pol_v,i_p_sp_pol_v,  &
&                                   iq_W,iq_W_s,ig_W,i_k_s_m1,iq_W_s_mq,ig_W_mq,i_kmq_s_m1,&
&                                   BS_n_g_W,O1,O2,O_times_W,i_p_s,i_pmq_s,q_W_rot)
           if (l_dir_alg)       H_c=K_correlation_kernel_dir(i_block,i_p,i_pmq, &
&                                   i_k_s,i_p_s, i_c_k,i_c_p,i_kmq_s,i_pmq_s  ,i_v_k,i_v_p,     &
&                                   i_kmq_t,i_pmq_t,i_k_sp_pol_c,i_p_sp_pol_c,i_k_sp_pol_v,i_p_sp_pol_v,  &
&                                   iq_W,iq_W_s,ig_W,i_k_s_m1,iq_W_s_mq,ig_W_mq,i_kmq_s_m1,&
&                                   BS_n_g_W,O1,O2,O_times_W)
         endif
         !         
       case ("C")
         !
         ! :::COUPLING TERM:::
         !=============================
         ! 
         ! This implementation is used only at iq=1 when l_BS_ares_from_res=.true.
         ! It uses only hald of the BS_T_grps, saving memory & cpu time for the oscillators
         ! Otherwise the exchange and tddft implementation are not corret
         ! The correlation imlpementation instead should be correct (since iv,ic
         ! indexes are properly inverted).
         !
         ! Exchange
         if (iHxc==1) H_x=K_exchange_kernel_coupling(iq, BS_n_g_exch,              &
&                                 BS_T_grp(i_Tgrp_p),i_Tp, BS_T_grp(i_Tgrp_k),i_Tk) 
         !
         ! ALDA
         if (iHxc==2) then
           if(l_tddft_rsum)     H_x=H_x+TDDFT_ALDA_eh_space_R_kernel(is_k,os_k,is_p,os_p,tddft_wf,'CPL')
           if(l_tddft_gsum)     H_x=H_x+TDDFT_ALDA_eh_space_G_kernel_coupling(BS_n_g_fxc,&
&                                 BS_T_grp(i_Tgrp_p),i_Tp, BS_T_grp(i_Tgrp_k),i_Tk)
         endif
         !
         ! Correlations
         if (iHxc==3) then
           ! To check for magnons
           ! When doing correlation i invert i_c_p and i_v_p
           ! Here also i_p_sp_pol_c and i_p_sp_pol_v are inverted ...
           ! q of the screened interaction in the BZ frame of the oscillators, R_W q_W (Cartesian)
           q_W_rot=matmul(rl_sop(:,:,iq_W_s),q%pt(iq_W,:))
           call c2a(v_in=q_W_rot,mode='ki2c')
           if (l_std_alg)       H_c=K_correlation_kernel_std(i_block,i_p,i_pmq, &
&                                 i_k_s,i_kp_s,i_c_k,i_v_p,i_kmq_s,i_kp_mq_s,i_v_k,i_c_p,    &
&                                 i_kmq_t,i_pmq_t,i_k_sp_pol_c,i_p_sp_pol_v,i_k_sp_pol_v,i_p_sp_pol_c,  &
&                                 iq_W,iq_W_s,ig_W,i_k_s_m1,iq_W_s_mq,ig_W_mq,i_kmq_s_m1,&
&                                 BS_n_g_W,O1,O2,O_times_W,i_p_s,i_pmq_s,q_W_rot)
           if (l_dir_alg)       H_c=K_correlation_kernel_dir(i_block,i_p,i_pmq, &
&                                 i_k_s,i_p_s ,i_c_k,i_v_p,i_kmq_s,i_pmq_s  ,i_v_k,i_c_p,    &
&                                 i_kmq_t,i_pmq_t,i_k_sp_pol_c,i_p_sp_pol_v,i_k_sp_pol_v,i_p_sp_pol_c,  &
&                                 iq_W,iq_W_s,ig_W,i_k_s_m1,iq_W_s_mq,ig_W_mq,i_kmq_s_m1,&
&                                 BS_n_g_W,O1,O2,O_times_W)
         endif
         !
       end select
       !
       if (l_tddft.and.WF_exx_fraction>0._SP.and.iHxc==3) H_c=H_c*WF_exx_fraction
       !
       sqrt_f_itk=sqrt(cmplx(BS_T_grp(i_Tgrp_k)%f(i_Tk),kind=SP))
       sqrt_f_itp=sqrt(cmplx(BS_T_grp(i_Tgrp_p)%f(i_Tp),kind=SP))
       !
       BS_mat_tmp=sqrt_f_itk*(H_x*Co-H_c)*sqrt_f_itp
       if (mode_now=="C") BS_mat_tmp= cI*BS_mat_tmp
       !
       if (     l_matrix_init) BS_blk(i_block)%mat(i_Tk,i_Tp) = BS_mat_tmp
       if (.not.l_matrix_init) BS_blk(i_block)%mat(i_Tk,i_Tp) = BS_blk(i_block)%mat(i_Tk,i_Tp)+BS_mat_tmp
       !
       if (l_write_kernel) BS_blk(i_block)%done(i_Tk,i_Tp)="t"
       !
     enddo   ! i_Tk
   enddo     ! i_Tp
   !
   !DEV_OMP end do
   !
   if(iHxc==2) then
     !
     if (l_tddft_rsum) then
       YAMBO_FREE(tddft_wf%rhotwr1)
       YAMBO_FREE(tddft_wf%rhotwr2)
       YAMBO_FREE(tddft_wf%WF_symm1)
       YAMBO_FREE(tddft_wf%WF_symm2)
     endif
     !
   endif
   !
   if (iHxc==3) then
     YAMBO_FREE_GPU(O1)
     YAMBO_FREE_GPU(O2)
     YAMBO_FREE_GPU(O_times_W)
     YAMBO_FREE(O1)
     YAMBO_FREE(O2)
     YAMBO_FREE(O_times_W)
   endif
   !
   !DEV_OMP end parallel
!
end subroutine K_kernel_default
