@@ -22,6 +22,7 @@ DensityMatrix<TK, TR>::~DensityMatrix()
2222 {
2323 delete it;
2424 }
25+ delete[] this ->dmr_tmp_ ;
2526}
2627
2728template <typename TK, typename TR>
@@ -627,12 +628,12 @@ void DensityMatrix<std::complex<double>, double>::cal_DMR_full(hamilt::HContaine
627628#endif
628629 // loop over k-points
629630 // calculate full matrix for complex density matrix
630- for (int ik = 0 ; ik < this ->_nks ; ++ik)
631+ for (int ik = 0 ; ik < this ->_nk ; ++ik)
631632 {
632633 // cal k_phase
633634 // if TK==std::complex<double>, kphase is e^{ikR}
634635 const ModuleBase::Vector3<double > dR (r_index[0 ], r_index[1 ], r_index[2 ]);
635- const double arg = (this ->_kv -> kvec_d [ik] * dR) * ModuleBase::TWO_PI;
636+ const double arg = (this ->_kvec_d [ik] * dR) * ModuleBase::TWO_PI;
636637 double sinp, cosp;
637638 ModuleBase::libm::sincos (arg, &sinp, &cosp);
638639 std::complex <double > kphase = std::complex <double >(cosp, sinp);
@@ -983,6 +984,82 @@ void DensityMatrix<std::complex<double>, double>::write_DMK(const std::string di
983984 ofs.close ();
984985}
985986
987+ // switch_dmr
988+ template <typename TK, typename TR>
989+ void DensityMatrix<TK, TR>::switch_dmr(const int mode)
990+ {
991+ ModuleBase::TITLE (" DensityMatrix" , " switch_dmr" );
992+ if (this ->_nspin != 2 )
993+ {
994+ return ;
995+ }
996+ else
997+ {
998+ ModuleBase::timer::tick (" DensityMatrix" , " switch_dmr" );
999+ switch (mode)
1000+ {
1001+ case 0 :
1002+ // switch to original density matrix
1003+ if (this ->dmr_tmp_ != nullptr && this ->dmr_origin_ .size () != 0 )
1004+ {
1005+ this ->_DMR [0 ]->allocate (this ->dmr_origin_ .data (), false );
1006+ delete[] this ->dmr_tmp_ ;
1007+ this ->dmr_tmp_ = nullptr ;
1008+ }
1009+ // else: do nothing
1010+ break ;
1011+ case 1 :
1012+ // switch to total magnetization density matrix, dmr_up + dmr_down
1013+ if (this ->dmr_tmp_ == nullptr )
1014+ {
1015+ const size_t size = this ->_DMR [0 ]->get_nnr ();
1016+ this ->dmr_tmp_ = new TR[size];
1017+ this ->dmr_origin_ .resize (size);
1018+ for (int i = 0 ; i < size; ++i)
1019+ {
1020+ this ->dmr_tmp_ [i] = this ->dmr_origin_ [i] + this ->_DMR [1 ]->get_wrapper ()[i];
1021+ }
1022+ this ->_DMR [0 ]->allocate (this ->dmr_tmp_ , false );
1023+ }
1024+ else
1025+ {
1026+ const size_t size = this ->_DMR [0 ]->get_nnr ();
1027+ for (int i = 0 ; i < size; ++i)
1028+ {
1029+ this ->dmr_tmp_ [i] = this ->dmr_origin_ [i] - this ->_DMR [1 ]->get_wrapper ()[i];
1030+ }
1031+ }
1032+ break ;
1033+ case 2 :
1034+ // switch to magnetization density matrix, dmr_up - dmr_down
1035+ if (this ->dmr_tmp_ == nullptr )
1036+ {
1037+ const size_t size = this ->_DMR [0 ]->get_nnr ();
1038+ this ->dmr_tmp_ = new TR[size];
1039+ this ->dmr_origin_ .resize (size);
1040+ for (int i = 0 ; i < size; ++i)
1041+ {
1042+ this ->dmr_origin_ [i] = this ->_DMR [0 ]->get_wrapper ()[i];
1043+ this ->dmr_tmp_ [i] = this ->dmr_origin_ [i] - this ->_DMR [1 ]->get_wrapper ()[i];
1044+ }
1045+ this ->_DMR [0 ]->allocate (this ->dmr_tmp_ , false );
1046+ }
1047+ else
1048+ {
1049+ const size_t size = this ->_DMR [0 ]->get_nnr ();
1050+ for (int i = 0 ; i < size; ++i)
1051+ {
1052+ this ->dmr_tmp_ [i] = this ->dmr_origin_ [i] - this ->_DMR [1 ]->get_wrapper ()[i];
1053+ }
1054+ }
1055+ break ;
1056+ default :
1057+ throw std::string (" Unknown mode in switch_dmr" );
1058+ }
1059+ ModuleBase::timer::tick (" DensityMatrix" , " switch_dmr" );
1060+ }
1061+ }
1062+
9861063// T of HContainer can be double or complex<double>
9871064template class DensityMatrix <double , double >; // Gamma-Only case
9881065template class DensityMatrix <std::complex <double >, double >; // Multi-k case
0 commit comments