diff --git a/source/source_io/module_ctrl/ctrl_output_td.cpp b/source/source_io/module_ctrl/ctrl_output_td.cpp index 78ca6fe34a..bdc4282cf9 100644 --- a/source/source_io/module_ctrl/ctrl_output_td.cpp +++ b/source/source_io/module_ctrl/ctrl_output_td.cpp @@ -51,7 +51,9 @@ void ctrl_output_td(const UnitCell& ucell, } else if (TD_info::out_current == 2) { - ModuleIO::write_current(ucell, grid, istep, psi, pelec, kv, pv, orb, td_p, p_hamilt->getSR(), p_hamilt->getHR(), exx_nao, exx_info); + const hamilt::HContainer* sR = p_hamilt->getSR(); + const std::vector*> spin_hR = p_hamilt->getHR_vector(); + ModuleIO::write_current(ucell, grid, istep, psi, pelec, kv, pv, orb, td_p, sR, spin_hR, exx_nao, exx_info); } #endif // __LCAO } diff --git a/source/source_io/module_current/td_current_io.h b/source/source_io/module_current/td_current_io.h index 69ce2dc030..e0f85638c6 100644 --- a/source/source_io/module_current/td_current_io.h +++ b/source/source_io/module_current/td_current_io.h @@ -53,7 +53,7 @@ void write_current( const LCAO_Orbitals& orb, TD_info* td_p, const hamilt::HContainer* sR, - const hamilt::HContainer* hR, + const std::vector*>& spin_hR, const Exx_NAO>& exx_nao, const Exx_Info& exx_info ); @@ -95,7 +95,7 @@ void cal_velocity_basis_k(const UnitCell& ucell, const K_Vectors& kv, const ModuleBase::Vector3*>& rR, const hamilt::HContainer& sR, - const hamilt::HContainer>& hR, + const std::vector>*>& spin_hR, std::vector*>>& velocity_basis_k); void cal_velocity_matrix(const psi::Psi>* psi, @@ -111,7 +111,7 @@ void cal_current_comm_k(const UnitCell& ucell, const K_Vectors& kv, TD_info* td_p, const hamilt::HContainer& sR, - const hamilt::HContainer>& hR, + const std::vector>*>& spin_hR, const psi::Psi>* psi, const elecstate::ElecState* pelec, std::vector>& current_k); diff --git a/source/source_io/module_current/td_current_io_comm.cpp b/source/source_io/module_current/td_current_io_comm.cpp index 12da24fd30..059dd69aa2 100644 --- a/source/source_io/module_current/td_current_io_comm.cpp +++ b/source/source_io/module_current/td_current_io_comm.cpp @@ -12,6 +12,8 @@ #include "source_lcao/module_rt/td_folding.h" #include "source_lcao/module_rt/td_info.h" #include "td_current_io.h" +#include +#include #ifdef __EXX #include "source_lcao/module_operator_lcao/op_exx_lcao.h" #include "source_lcao/module_ri/exx_lri.h" @@ -257,7 +259,7 @@ void ModuleIO::cal_velocity_basis_k(const UnitCell& ucell, const K_Vectors& kv, const ModuleBase::Vector3*>& rR, const hamilt::HContainer& sR, - const hamilt::HContainer>& hR, + const std::vector>*>& spin_hR, std::vector*>>& velocity_basis_k) { ModuleBase::TITLE("ModuleIO", "cal_velocity_basis_k"); @@ -284,6 +286,7 @@ void ModuleIO::cal_velocity_basis_k(const UnitCell& ucell, for (size_t ik = 0; ik != kv.get_nks(); ++ik) { + const hamilt::HContainer>& hR = *spin_hR[kv.isk[ik]]; // set H(k), S(k) // 1.1 set H(k) ModuleBase::GlobalFunc::ZEROS(hk, pv->para_2d().size()); @@ -615,7 +618,7 @@ void ModuleIO::cal_current_comm_k(const UnitCell& ucell, const K_Vectors& kv, TD_info* td_p, const hamilt::HContainer& sR, - const hamilt::HContainer>& hR, + const std::vector>*>& spin_hR, const psi::Psi>* psi, const elecstate::ElecState* pelec, std::vector>& current_k) @@ -642,9 +645,9 @@ void ModuleIO::cal_current_comm_k(const UnitCell& ucell, } } // set rR - set_rR_from_hR(ucell, GridD, orb, pv, td_p->r_calculator, &hR, rR); + set_rR_from_hR(ucell, GridD, orb, pv, td_p->r_calculator, spin_hR.front(), rR); // set velocity_basis_k - cal_velocity_basis_k(ucell, td_p, orb, pv, kv, rR, sR, hR, velocity_basis_k); + cal_velocity_basis_k(ucell, td_p, orb, pv, kv, rR, sR, spin_hR, velocity_basis_k); // set velocity_k cal_velocity_matrix(psi, pv, kv, velocity_basis_k, velocity_k); @@ -689,7 +692,7 @@ void ModuleIO::write_current(const UnitCell& ucell, const LCAO_Orbitals& orb, TD_info* td_p, const hamilt::HContainer* sR, - const hamilt::HContainer* hR, + const std::vector*>& spin_hR, const Exx_NAO>& exx_nao, const Exx_Info& exx_info) { @@ -700,12 +703,19 @@ void ModuleIO::write_current(const UnitCell& ucell, double omega = ucell.omega; std::vector> current_k; - hamilt::HContainer>* full_hR; - full_hR = new hamilt::HContainer>(pv); + std::vector>>> full_hR; + std::vector>*> current_hR; + for (const hamilt::HContainer* hR : spin_hR) + { + std::unique_ptr>> matrix( + new hamilt::HContainer>(pv)); + hamilt::HContainer>* matrix_ptr = matrix.get(); + sum_HR(ucell, *pv, kv, hR, matrix_ptr, exx_nao, exx_info, *td_p); + current_hR.push_back(matrix_ptr); + full_hR.push_back(std::move(matrix)); + } current_k.resize(kv.get_nks()); - sum_HR(ucell, *pv, kv, hR, full_hR, exx_nao, exx_info, *td_p); - cal_current_comm_k(ucell, GridD, orb, pv, kv, td_p, *sR, *full_hR, psi, pelec, current_k); - delete full_hR; + cal_current_comm_k(ucell, GridD, orb, pv, kv, td_p, *sR, current_hR, psi, pelec, current_k); int nspin0 = 1; if (PARAM.inp.nspin == 2) @@ -772,7 +782,7 @@ template void ModuleIO::write_current(const UnitCell& ucell, const LCAO_Orbitals& orb, TD_info* td_p, const hamilt::HContainer* sR, - const hamilt::HContainer* hR, + const std::vector*>& spin_hR, const Exx_NAO>& exx_nao, const Exx_Info& exx_info); @@ -786,7 +796,7 @@ template void ModuleIO::write_current>(const UnitCell& ucel const LCAO_Orbitals& orb, TD_info* td_p, const hamilt::HContainer>* sR, - const hamilt::HContainer>* hR, + const std::vector>*>& spin_hR, const Exx_NAO>& exx_nao, const Exx_Info& exx_info); #endif //__LCAO diff --git a/source/source_lcao/hamilt_lcao.cpp b/source/source_lcao/hamilt_lcao.cpp index 0949d98882..292e71b42e 100644 --- a/source/source_lcao/hamilt_lcao.cpp +++ b/source/source_lcao/hamilt_lcao.cpp @@ -177,6 +177,10 @@ std::vector*> HamiltLCAO::getHR_vector() TR* down_data = up_data + nnr; this->hr_spin_up_.reset(new HContainer(*this->hR, up_data)); this->hr_spin_dn_.reset(new HContainer(*this->hR, down_data)); + if (!this->vl_in_h) + { + return {this->hR.get(), this->hR.get()}; + } return {this->hr_spin_up_.get(), this->hr_spin_dn_.get()}; } else diff --git a/source/source_lcao/hamilt_lcao.h b/source/source_lcao/hamilt_lcao.h index e446ba5cd7..3485174615 100644 --- a/source/source_lcao/hamilt_lcao.h +++ b/source/source_lcao/hamilt_lcao.h @@ -148,7 +148,7 @@ class HamiltLCAO : public Hamilt std::vector& getHRS2() { return this->hRS2; } /// Get HR as a vector of HContainer pointers (one per spin). - /// For nspin=2, returns pointers to internally managed per-spin wrappers over hRS2. + /// For nspin=2, vl_in_h=0 shares the active H(R), matching updateHk. /// Returned pointers are owned by this class; caller must NOT delete them. std::vector*> getHR_vector(); diff --git a/tests/08_RI/CASES_CPU.txt b/tests/08_RI/CASES_CPU.txt index c783fa2329..1c51dd45fd 100644 --- a/tests/08_RI/CASES_CPU.txt +++ b/tests/08_RI/CASES_CPU.txt @@ -22,6 +22,7 @@ rpa_scf_gamma rpa_scf_shrink_multik rt_tddft_pbe0_gamma rt_tddft_hse_spin2 +rt_tddft_hse_current_spin2_up bse_lr_multik nscf_hse_multik diff --git a/tests/08_RI/rt_tddft_hse_current_spin2_up/INPUT b/tests/08_RI/rt_tddft_hse_current_spin2_up/INPUT new file mode 100644 index 0000000000..52c1da7837 --- /dev/null +++ b/tests/08_RI/rt_tddft_hse_current_spin2_up/INPUT @@ -0,0 +1,46 @@ +INPUT_PARAMETERS +calculation md +esolver_type tddft +md_type nve +estep_per_md 2 +td_dt 0.005 +md_tfirst 0 +td_vext 1 # add time-dependent external potential +td_vext_dire 3 # direction along z +td_stype 2 # 2: hybrid gauge +td_ttype 3 # Heaviside type potential +td_tstart 1 # the step electric field starts +td_tend 10 # the step electric field ends +td_heavi_t0 3 +td_heavi_amp 0.05 +pseudo_dir ../../PP_ORB +orbital_dir ../../PP_ORB +basis_type lcao +gamma_only 0 +ecutwfc 5 +ks_solver scalapack_gvx +mixing_type broyden +mixing_beta 0.7 +mixing_gg0 0.0 +dft_functional hse +exx_singularity_correction limits +symmetry -1 +suffix autotest +fixed_atoms 1 +cal_force 0 +dump_force 0 +scf_thr 1e-6 +scf_nmax 150 +md_restartfreq 1 +out_wfc_lcao 0 +out_app_flag 0 +out_freq_td 1 +out_efield 0 +out_vecpot 0 +out_current 2 +md_nstep 1 +nspin 2 +exx_real_number 0 +out_current_k 1 +nbands 8 +nupdown 2 diff --git a/tests/08_RI/rt_tddft_hse_current_spin2_up/KPT b/tests/08_RI/rt_tddft_hse_current_spin2_up/KPT new file mode 100644 index 0000000000..e769af7638 --- /dev/null +++ b/tests/08_RI/rt_tddft_hse_current_spin2_up/KPT @@ -0,0 +1,4 @@ +K_POINTS +0 +Gamma +2 1 1 0 0 0 diff --git a/tests/08_RI/rt_tddft_hse_current_spin2_up/README b/tests/08_RI/rt_tddft_hse_current_spin2_up/README new file mode 100644 index 0000000000..cdb6ec114c --- /dev/null +++ b/tests/08_RI/rt_tddft_hse_current_spin2_up/README @@ -0,0 +1 @@ +Fixed-ion Si HSE/hybrid-gauge commutator current with collinear spin polarization. diff --git a/tests/08_RI/rt_tddft_hse_current_spin2_up/STRU b/tests/08_RI/rt_tddft_hse_current_spin2_up/STRU new file mode 100644 index 0000000000..9a24d623b6 --- /dev/null +++ b/tests/08_RI/rt_tddft_hse_current_spin2_up/STRU @@ -0,0 +1,22 @@ +ATOMIC_SPECIES +Si 28.085 Si_dojo_nsoc.upf + +NUMERICAL_ORBITAL +Si_dojo_6au_sz.orb + +LATTICE_CONSTANT +10.2 // add lattice constant + +LATTICE_VECTORS +0.0 0.5 0.5 +0.5 0.0 0.5 +0.5 0.5 0.0 + +ATOMIC_POSITIONS +Cartesian //Cartesian or Direct coordinate. + +Si // Element type +1.0 // magnetism +2 // number of atoms +0.00 0.00 0.00 0 0 0 +0.25 0.25 0.25 0 0 0 diff --git a/tests/08_RI/rt_tddft_hse_current_spin2_up/current_tot.txt.ref b/tests/08_RI/rt_tddft_hse_current_spin2_up/current_tot.txt.ref new file mode 100644 index 0000000000..ab31681f0f --- /dev/null +++ b/tests/08_RI/rt_tddft_hse_current_spin2_up/current_tot.txt.ref @@ -0,0 +1,3 @@ + 1 -4.089080634814e-19 4.090305385391e-19 4.090942654256e-19 + 2 -3.550737228392e-07 3.550678480581e-07 1.373779434519e-06 + 3 6.311340786252e-07 -6.311437488938e-07 1.015098378634e-05 diff --git a/tests/08_RI/rt_tddft_hse_current_spin2_up/result.ref b/tests/08_RI/rt_tddft_hse_current_spin2_up/result.ref new file mode 100644 index 0000000000..d1448e523d --- /dev/null +++ b/tests/08_RI/rt_tddft_hse_current_spin2_up/result.ref @@ -0,0 +1,4 @@ +etotref -190.6136471104446 +etotperatomref -95.3068235552 +CompareCurrent_pass 0 +totaltimeref 1 diff --git a/tests/integrate/validation_tools/props_tddft.sh b/tests/integrate/validation_tools/props_tddft.sh index e9af976d15..9fff21f490 100644 --- a/tests/integrate/validation_tools/props_tddft.sh +++ b/tests/integrate/validation_tools/props_tddft.sh @@ -24,6 +24,9 @@ run_tddft_props() { if ! test -z "$out_current" && [ $out_current ]; then current1ref=current_tot.txt.ref current1cal=OUT.autotest/current_tot.txt + if [ "$out_current" == 2 ]; then + current1cal=OUT.autotest/current_tot_comm.txt + fi local current_tol=1e-10 if [ -f threshold ]; then local configured_tol @@ -32,8 +35,18 @@ run_tddft_props() { current_tol=$configured_tol fi fi - python3 "$COMPARE_SCRIPT" "$current1ref" "$current1cal" -tol "$current_tol" - echo "CompareCurrent_pass $?" >>$props_result_file + local current_status=1 + if [ -f "$current1ref" ] && [ -f "$current1cal" ]; then + local reference_rows + local current_rows + reference_rows=$(awk 'NF { count++ } END { print count+0 }' "$current1ref") + current_rows=$(awk 'NF { count++ } END { print count+0 }' "$current1cal") + if [ "$reference_rows" -gt 0 ] && [ "$current_rows" -ge "$reference_rows" ]; then + python3 "$COMPARE_SCRIPT" "$current1ref" "$current1cal" -tol "$current_tol" + current_status=$? + fi + fi + echo "CompareCurrent_pass $current_status" >>$props_result_file fi #--------------------------------------------