@@ -24,62 +24,24 @@ DensityMatrix<TK, TR>::~DensityMatrix()
2424 }
2525}
2626
27- // constructor for multi-k
2827template <typename TK, typename TR>
29- DensityMatrix<TK, TR>::DensityMatrix(const K_Vectors* kv_in, const Parallel_Orbitals* paraV_in, const int nspin)
28+ DensityMatrix<TK, TR>::DensityMatrix(const Parallel_Orbitals* paraV_in, const int nspin, const std::vector<ModuleBase::Vector3<double >>& kvec_d, const int nk)
29+ : _paraV(paraV_in), _nspin(nspin), _kvec_d(kvec_d), _nks((nk > 0 && nk <= _kvec_d.size()) ? nk : _kvec_d.size())
3030{
3131 ModuleBase::TITLE (" DensityMatrix" , " DensityMatrix-MK" );
32- this ->_kv = kv_in;
33- this ->_paraV = paraV_in;
34- // set this->_nspin
35- if (nspin == 1 || nspin == 4 )
36- {
37- this ->_nspin = 1 ;
38- }
39- else if (nspin == 2 )
40- {
41- this ->_nspin = 2 ;
42- #ifdef __DEBUG
43- assert (kv_in->get_nks () % 2 == 0 );
44- #endif
45- }
46- else
47- {
48- throw std::string (" nspin must be 1, 2 or 4" );
49- }
50- // set this->_nks, which is real number of k-points
51- this ->_nks = kv_in->get_nks () / this ->_nspin ;
52- // allocate memory for _DMK
53- this ->_DMK .resize (this ->_kv ->get_nks ());
54- for (int ik = 0 ; ik < this ->_kv ->get_nks (); ik++)
32+ const int nks = _nks * _nspin;
33+ this ->_DMK .resize (nks);
34+ for (int ik = 0 ; ik < nks; ik++)
5535 {
5636 this ->_DMK [ik].resize (this ->_paraV ->get_row_size () * this ->_paraV ->get_col_size ());
5737 }
5838 ModuleBase::Memory::record (" DensityMatrix::DMK" , this ->_DMK .size () * this ->_DMK [0 ].size () * sizeof (TK));
5939}
6040
61- // constructor for Gamma-Only
6241template <typename TK, typename TR>
63- DensityMatrix<TK, TR>::DensityMatrix(const Parallel_Orbitals* paraV_in, const int nspin)
42+ DensityMatrix<TK, TR>::DensityMatrix(const Parallel_Orbitals* paraV_in, const int nspin) :_paraV(paraV_in), _nspin(nspin), _kvec_d({ ModuleBase::Vector3< double >( 0 , 0 , 0 ) }), _nks( 1 )
6443{
6544 ModuleBase::TITLE (" DensityMatrix" , " DensityMatrix-GO" );
66- this ->_paraV = paraV_in;
67- // set this->_nspin
68- if (nspin == 1 || nspin == 4 )
69- {
70- this ->_nspin = 1 ;
71- }
72- else if (nspin == 2 )
73- {
74- this ->_nspin = 2 ;
75- }
76- else
77- {
78- throw std::string (" nspin must be 1, 2 or 4" );
79- }
80- // set this->_nks, which is real number of k-points
81- this ->_nks = 1 ;
82- // allocate memory for _DMK
8345 this ->_DMK .resize (_nspin);
8446 for (int ik = 0 ; ik < this ->_nspin ; ik++)
8547 {
@@ -328,10 +290,9 @@ template <typename TK, typename TR>
328290int DensityMatrix<TK, TR>::get_DMK_nks() const
329291{
330292#ifdef __DEBUG
331- assert (this ->_DMK .size () != 0 );
332- assert (this ->_kv != nullptr );
293+ assert (this ->_DMK .size () == _nks * _nspin);
333294#endif
334- return this -> _kv -> get_nks () ;
295+ return _nks * _nspin ;
335296}
336297
337298template <typename TK, typename TR>
@@ -440,7 +401,7 @@ void DensityMatrix<TK, TR>::cal_DMR_test()
440401 // cal k_phase
441402 // if TK==std::complex<double>, kphase is e^{ikR}
442403 const ModuleBase::Vector3<double > dR (r_index[0 ], r_index[1 ], r_index[2 ]);
443- const double arg = (this ->_kv -> kvec_d [ik] * dR) * ModuleBase::TWO_PI;
404+ const double arg = (this ->_kvec_d [ik] * dR) * ModuleBase::TWO_PI;
444405 double sinp, cosp;
445406 ModuleBase::libm::sincos (arg, &sinp, &cosp);
446407 std::complex <double > kphase = std::complex <double >(cosp, sinp);
@@ -515,7 +476,7 @@ void DensityMatrix<std::complex<double>, double>::cal_DMR()
515476 // cal k_phase
516477 // if TK==std::complex<double>, kphase is e^{ikR}
517478 const ModuleBase::Vector3<double > dR (r_index[0 ], r_index[1 ], r_index[2 ]);
518- const double arg = (this ->_kv -> kvec_d [ik] * dR) * ModuleBase::TWO_PI;
479+ const double arg = (this ->_kvec_d [ik] * dR) * ModuleBase::TWO_PI;
519480 double sinp, cosp;
520481 ModuleBase::libm::sincos (arg, &sinp, &cosp);
521482 std::complex <double > kphase = std::complex <double >(cosp, sinp);
@@ -561,7 +522,7 @@ void DensityMatrix<std::complex<double>, double>::cal_DMR()
561522 // cal k_phase
562523 // if TK==std::complex<double>, kphase is e^{ikR}
563524 const ModuleBase::Vector3<double > dR (r_index[0 ], r_index[1 ], r_index[2 ]);
564- const double arg = (this ->_kv -> kvec_d [ik] * dR) * ModuleBase::TWO_PI;
525+ const double arg = (this ->_kvec_d [ik] * dR) * ModuleBase::TWO_PI;
565526 double sinp, cosp;
566527 ModuleBase::libm::sincos (arg, &sinp, &cosp);
567528 std::complex <double > kphase = std::complex <double >(cosp, sinp);
@@ -678,7 +639,7 @@ void DensityMatrix<std::complex<double>, double>::cal_DMR(const int ik)
678639 // cal k_phase
679640 // if TK==std::complex<double>, kphase is e^{ikR}
680641 const ModuleBase::Vector3<double > dR (r_index[0 ], r_index[1 ], r_index[2 ]);
681- const double arg = (this ->_kv -> kvec_d [ik] * dR) * ModuleBase::TWO_PI;
642+ const double arg = (this ->_kvec_d [ik] * dR) * ModuleBase::TWO_PI;
682643 double sinp, cosp;
683644 ModuleBase::libm::sincos (arg, &sinp, &cosp);
684645 std::complex <double > kphase = std::complex <double >(cosp, sinp);
@@ -855,9 +816,9 @@ void DensityMatrix<TK, TR>::read_DMK(const std::string directory, const int ispi
855816 // quit the program or not.
856817 bool quit = false ;
857818
858- ModuleBase::CHECK_DOUBLE (ifs, this ->_kv -> kvec_d [ik].x , quit);
859- ModuleBase::CHECK_DOUBLE (ifs, this ->_kv -> kvec_d [ik].y , quit);
860- ModuleBase::CHECK_DOUBLE (ifs, this ->_kv -> kvec_d [ik].z , quit);
819+ ModuleBase::CHECK_DOUBLE (ifs, this ->_kvec_d [ik].x , quit);
820+ ModuleBase::CHECK_DOUBLE (ifs, this ->_kvec_d [ik].y , quit);
821+ ModuleBase::CHECK_DOUBLE (ifs, this ->_kvec_d [ik].z , quit);
861822 ModuleBase::CHECK_INT (ifs, this ->_paraV ->nrow );
862823 ModuleBase::CHECK_INT (ifs, this ->_paraV ->ncol );
863824 } // If file exist, read in data.
@@ -890,7 +851,7 @@ void DensityMatrix<double, double>::write_DMK(const std::string directory, const
890851 {
891852 ModuleBase::WARNING (" elecstate::write_dmk" , " Can't create DENSITY MATRIX File!" );
892853 }
893- ofs << this ->_kv -> kvec_d [ik].x << " " << this ->_kv -> kvec_d [ik].y << " " << this ->_kv -> kvec_d [ik].z << std::endl;
854+ ofs << this ->_kvec_d [ik].x << " " << this ->_kvec_d [ik].y << " " << this ->_kvec_d [ik].z << std::endl;
894855 ofs << " \n " << this ->_paraV ->nrow << " " << this ->_paraV ->ncol << std::endl;
895856
896857 ofs << std::setprecision (3 );
@@ -927,7 +888,7 @@ void DensityMatrix<std::complex<double>, double>::write_DMK(const std::string di
927888 {
928889 ModuleBase::WARNING (" elecstate::write_dmk" , " Can't create DENSITY MATRIX File!" );
929890 }
930- ofs << this ->_kv -> kvec_d [ik].x << " " << this ->_kv -> kvec_d [ik].y << " " << this ->_kv -> kvec_d [ik].z << std::endl;
891+ ofs << this ->_kvec_d [ik].x << " " << this ->_kvec_d [ik].y << " " << this ->_kvec_d [ik].z << std::endl;
931892 ofs << " \n " << this ->_paraV ->nrow << " " << this ->_paraV ->ncol << std::endl;
932893
933894 ofs << std::setprecision (3 );
0 commit comments