Skip to content

Commit 8b53097

Browse files
committed
Refactor class DensityMatrix. Make the code easier to read
1 parent a7dfa04 commit 8b53097

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

source/source_estate/module_dm/density_matrix.cpp

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void DensityMatrix<std::complex<double>, double>::cal_DMR(const int ik_in)
6060
using TR = double;
6161

6262
// To check whether DMR has been initialized
63-
assert(!this->_DMR.empty() && "DMR has not been initialized!");
63+
assert(this->_DMR.size()==this->_nspin && "DMR has not been initialized!");
6464

6565
ModuleBase::timer::tick("DensityMatrix", "cal_DMR");
6666
const int ld_hk = this->_paraV->nrow;
@@ -120,7 +120,6 @@ void DensityMatrix<std::complex<double>, double>::cal_DMR(const int ik_in)
120120
for(int ik = 0; ik < this->_nk; ++ik)
121121
{
122122
if(ik_in >= 0 && ik_in != ik) { continue; }
123-
124123
// copy column-major DMK to row-major DMK_mat_trans (for the purpose of computational efficiency)
125124
const std::complex<double>*const DMK_mat_ptr
126125
= this->_DMK[ik + ik_begin].data()
@@ -185,10 +184,10 @@ void DensityMatrix<std::complex<double>, double>::cal_DMR(const int ik_in)
185184
tmp[3] = tmp_DMR_mat[icol + step_trace[3]];
186185
// transfer to Pauli matrix and save the real part
187186
// save them back to the target_mat
188-
target_DMR_mat[icol + step_trace[0]] = tmp[0].real() + tmp[3].real(); // (rho_upup + rho_downdown).real()
189-
target_DMR_mat[icol + step_trace[1]] = tmp[1].real() + tmp[2].real(); // (rho_updown + rho_downup).real()
190-
target_DMR_mat[icol + step_trace[2]] = -tmp[1].imag() + tmp[2].imag(); // (i * (rho_updown - rho_downup)).real()
191-
target_DMR_mat[icol + step_trace[3]] = tmp[0].real() - tmp[3].real(); // (rho_upup - rho_downdown).real()
187+
target_DMR_mat[icol + step_trace[0]] = tmp[0].real() + tmp[3].real(); // rho_0 = (rho_upup + rho_downdown).real()
188+
target_DMR_mat[icol + step_trace[1]] = tmp[1].real() + tmp[2].real(); // rho_x = (rho_updown + rho_downup).real()
189+
target_DMR_mat[icol + step_trace[2]] = -tmp[1].imag() + tmp[2].imag(); // rho_y = (i * (rho_updown - rho_downup)).real()
190+
target_DMR_mat[icol + step_trace[3]] = tmp[0].real() - tmp[3].real(); // rho_z = (rho_upup - rho_downdown).real()
192191
}
193192
tmp_DMR_mat += col_size * 2;
194193
target_DMR_mat += col_size * 2;
@@ -207,7 +206,7 @@ void DensityMatrix<std::complex<double>, double>::cal_DMR_td(const UnitCell& uce
207206
ModuleBase::TITLE("DensityMatrix", "cal_DMR_td");
208207
using TR = double;
209208
// To check whether DMR has been initialized
210-
assert(!this->_DMR.empty() && "DMR has not been initialized!");
209+
assert(this->_DMR.size()==this->_nspin && "DMR has not been initialized!");
211210

212211
ModuleBase::timer::tick("DensityMatrix", "cal_DMR_td");
213212
const int ld_hk = this->_paraV->nrow;
@@ -249,10 +248,9 @@ void DensityMatrix<std::complex<double>, double>::cal_DMR_td(const UnitCell& uce
249248
}
250249
#endif
251250
target_DMR_mat_vec[iR] = target_mat->get_pointer();
252-
double arg_td = 0.0;
253251
//cal tddft phase for hybrid gauge
254-
ModuleBase::Vector3<double> dtau = ucell.cal_dtau(iat1, iat2, R_index);
255-
arg_td = At * dtau * ucell.lat0;
252+
const ModuleBase::Vector3<double> dtau = ucell.cal_dtau(iat1, iat2, R_index);
253+
const double arg_td = At * dtau * ucell.lat0;
256254
for(int ik = 0; ik < this->_nk; ++ik)
257255
{
258256
if(ik_in >= 0 && ik_in != ik) { continue; }
@@ -271,7 +269,6 @@ void DensityMatrix<std::complex<double>, double>::cal_DMR_td(const UnitCell& uce
271269
for(int ik = 0; ik < this->_nk; ++ik)
272270
{
273271
if(ik_in >= 0 && ik_in != ik) { continue; }
274-
275272
// copy column-major DMK to row-major DMK_mat_trans (for the purpose of computational efficiency)
276273
const std::complex<double>*const DMK_mat_ptr
277274
= this->_DMK[ik + ik_begin].data()
@@ -336,10 +333,10 @@ void DensityMatrix<std::complex<double>, double>::cal_DMR_td(const UnitCell& uce
336333
tmp[3] = tmp_DMR_mat[icol + step_trace[3]];
337334
// transfer to Pauli matrix and save the real part
338335
// save them back to the target_mat
339-
target_DMR_mat[icol + step_trace[0]] = tmp[0].real() + tmp[3].real(); // (rho_upup + rho_downdown).real()
340-
target_DMR_mat[icol + step_trace[1]] = tmp[1].real() + tmp[2].real(); // (rho_updown + rho_downup).real()
341-
target_DMR_mat[icol + step_trace[2]] = -tmp[1].imag() + tmp[2].imag(); // (i * (rho_updown - rho_downup)).real()
342-
target_DMR_mat[icol + step_trace[3]] = tmp[0].real() - tmp[3].real(); // (rho_upup - rho_downdown).real()
336+
target_DMR_mat[icol + step_trace[0]] = tmp[0].real() + tmp[3].real(); // rho_0 = (rho_upup + rho_downdown).real()
337+
target_DMR_mat[icol + step_trace[1]] = tmp[1].real() + tmp[2].real(); // rho_x = (rho_updown + rho_downup).real()
338+
target_DMR_mat[icol + step_trace[2]] = -tmp[1].imag() + tmp[2].imag(); // rho_y = (i * (rho_updown - rho_downup)).real()
339+
target_DMR_mat[icol + step_trace[3]] = tmp[0].real() - tmp[3].real(); // rho_z = (rho_upup - rho_downdown).real()
343340
}
344341
tmp_DMR_mat += col_size * 2;
345342
target_DMR_mat += col_size * 2;
@@ -353,23 +350,26 @@ void DensityMatrix<std::complex<double>, double>::cal_DMR_td(const UnitCell& uce
353350

354351
// calculate DMR from DMK using blas for multi-k calculation
355352
template <>
356-
void DensityMatrix<double, double>::cal_DMR_full(hamilt::HContainer<std::complex<double>>* dmR_out)const{}
353+
void DensityMatrix<double, double>::cal_DMR_full(hamilt::HContainer<std::complex<double>>* dmR_out, const int ik_in)const{}
357354
template <>
358-
void DensityMatrix<std::complex<double>, double>::cal_DMR_full(hamilt::HContainer<std::complex<double>>* dmR_out)const
355+
void DensityMatrix<std::complex<double>, double>::cal_DMR_full(
356+
hamilt::HContainer<std::complex<double>>* dmR_out,
357+
const int ik_in) const
359358
{
360359
ModuleBase::TITLE("DensityMatrix", "cal_DMR_full");
360+
using TR = std::complex<double>;
361361

362362
ModuleBase::timer::tick("DensityMatrix", "cal_DMR_full");
363363
const int ld_hk = this->_paraV->nrow;
364-
hamilt::HContainer<std::complex<double>>* target_DMR = dmR_out;
364+
hamilt::HContainer<TR>* target_DMR = dmR_out;
365365
// set zero since this function is called in every scf step
366366
target_DMR->set_zero();
367367
#ifdef _OPENMP
368368
#pragma omp parallel for schedule(dynamic)
369369
#endif
370370
for (int i = 0; i < target_DMR->size_atom_pairs(); ++i)
371371
{
372-
hamilt::AtomPair<std::complex<double>>& target_ap = target_DMR->get_atom_pair(i);
372+
hamilt::AtomPair<TR>& target_ap = target_DMR->get_atom_pair(i);
373373
const int iat1 = target_ap.get_atom_i();
374374
const int iat2 = target_ap.get_atom_j();
375375
// get global indexes of whole matrix for each atom in this process
@@ -379,14 +379,15 @@ void DensityMatrix<std::complex<double>, double>::cal_DMR_full(hamilt::HContaine
379379
const int col_size = this->_paraV->get_col_size(iat2);
380380
const int mat_size = row_size * col_size;
381381
const int R_size = target_ap.get_R_size();
382+
assert(row_ap != -1 && col_ap != -1 && "Atom-pair not belong this process");
382383

383384
// calculate kphase and target_mat_ptr
384385
std::vector<std::vector<std::complex<double>>> kphase_vec(this->_nk, std::vector<std::complex<double>>(R_size));
385-
std::vector<std::complex<double>*> target_DMR_mat_vec(R_size);
386+
std::vector<TR*> target_DMR_mat_vec(R_size);
386387
for(int iR = 0; iR < R_size; ++iR)
387388
{
388389
const ModuleBase::Vector3<int> R_index = target_ap.get_R_index(iR);
389-
hamilt::BaseMatrix<std::complex<double>>*const target_mat = target_ap.find_matrix(R_index);
390+
hamilt::BaseMatrix<TR>*const target_mat = target_ap.find_matrix(R_index);
390391
#ifdef __DEBUG
391392
if (target_mat == nullptr)
392393
{
@@ -397,6 +398,7 @@ void DensityMatrix<std::complex<double>, double>::cal_DMR_full(hamilt::HContaine
397398
target_DMR_mat_vec[iR] = target_mat->get_pointer();
398399
for(int ik = 0; ik < this->_nk; ++ik)
399400
{
401+
if(ik_in >= 0 && ik_in != ik) { continue; }
400402
// cal k_phase
401403
// if TK==std::complex<double>, kphase is e^{ikR}
402404
const ModuleBase::Vector3<double> dR(R_index[0], R_index[1], R_index[2]);
@@ -410,6 +412,7 @@ void DensityMatrix<std::complex<double>, double>::cal_DMR_full(hamilt::HContaine
410412
std::vector<std::complex<double>> DMK_mat_trans(mat_size);
411413
for(int ik = 0; ik < this->_nk; ++ik)
412414
{
415+
if(ik_in >= 0 && ik_in != ik) { continue; }
413416
// copy column-major DMK to row-major DMK_mat_trans (for the purpose of computational efficiency)
414417
const std::complex<double>*const DMK_mat_ptr
415418
= this->_DMK[ik].data()
@@ -422,12 +425,11 @@ void DensityMatrix<std::complex<double>, double>::cal_DMR_full(hamilt::HContaine
422425
for(int iR = 0; iR < R_size; ++iR)
423426
{
424427
const std::complex<double> kphase = kphase_vec[ik][iR];
425-
std::complex<double>* target_DMR_mat = target_DMR_mat_vec[iR];
426428
BlasConnector::axpy(mat_size,
427429
kphase,
428430
DMK_mat_trans.data(),
429431
1,
430-
target_DMR_mat,
432+
target_DMR_mat_vec[iR],
431433
1);
432434
}
433435
}

source/source_estate/module_dm/density_matrix.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,15 @@ class DensityMatrix
179179

180180
/**
181181
* @brief calculate density matrix DMR from dm(k) using blas::axpy
182+
* @param ik_in
182183
* if ik_in < 0, calculate all k-points
183184
* if ik_in >= 0, calculate only one k-point without summing over k-points
184185
*/
185186
void cal_DMR(const int ik_in = -1);
186187

187188
/**
188189
* @brief calculate density matrix DMR with additional vector potential phase, used for hybrid gauge tddft
190+
* @param ik_in
189191
* if ik_in < 0, calculate all k-points
190192
* if ik_in >= 0, calculate only one k-point without summing over k-points
191193
*/
@@ -195,8 +197,11 @@ class DensityMatrix
195197
* @brief calculate complex density matrix DMR with both real and imaginary part for noncollinear-spin calculation
196198
* the stored dm(k) has been used to calculate the passin DMR
197199
* @param dmR_out pointer of HContainer object to store the calculated complex DMR
200+
* @param ik_in
201+
* if ik_in < 0, calculate all k-points
202+
* if ik_in >= 0, calculate only one k-point without summing over k-points
198203
*/
199-
void cal_DMR_full(hamilt::HContainer<std::complex<double>>* dmR_out) const;
204+
void cal_DMR_full(hamilt::HContainer<std::complex<double>>* dmR_out, const int ik_in = -1) const;
200205

201206
/**
202207
* @brief (Only nspin=2) switch DMR to total density matrix or magnetization density matrix

0 commit comments

Comments
 (0)