Skip to content

Commit 3615265

Browse files
author
dyzheng
committed
feature: modify out_mat_ds, out_mat_t, out_mat_r, out_mat_xc2 for new CSR format
1 parent 20f36a6 commit 3615265

10 files changed

Lines changed: 351 additions & 40 deletions

File tree

source/source_esolver/esolver_gets.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,14 @@ void ESolver_GetS::runner(UnitCell& ucell, const int istep)
127127

128128
ModuleIO::output_SR(pv, gd, this->p_hamilt, fn);
129129

130-
if (PARAM.inp.out_mat_r)
130+
if (PARAM.inp.out_mat_r[0])
131131
{
132132
cal_r_overlap_R r_matrix;
133133
r_matrix.init(ucell, pv, orb_);
134134
r_matrix.out_rR(ucell, gd, istep);
135135
}
136136

137-
if (PARAM.inp.out_mat_ds)
137+
if (PARAM.inp.out_mat_ds[0])
138138
{
139139
LCAO_HS_Arrays HS_Arrays; // store sparse arrays
140140
//! Print out sparse matrix

source/source_io/module_ctrl/ctrl_runner_lcao.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void ctrl_runner_lcao(UnitCell& ucell, // unitcell
7676
);
7777
}
7878

79-
if (inp.out_mat_xc2)
79+
if (inp.out_mat_xc2[0])
8080
{
8181
ModuleIO::write_Vxc_R<TK, TR>(inp.nspin,
8282
&pv,

source/source_io/module_ctrl/ctrl_scf_lcao.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,10 @@ void ModuleIO::ctrl_scf_lcao(UnitCell& ucell,
237237
//------------------------------------------------------------------
238238
hamilt::Hamilt<TK>* p_ham_tk = static_cast<hamilt::Hamilt<TK>*>(p_hamilt);
239239

240-
ModuleIO::output_mat_sparse(inp.out_mat_dh,
241-
inp.out_mat_ds,
242-
inp.out_mat_t,
243-
inp.out_mat_r,
240+
ModuleIO::output_mat_sparse(inp.out_mat_dh[0],
241+
inp.out_mat_ds[0],
242+
inp.out_mat_t[0],
243+
inp.out_mat_r[0],
244244
istep,
245245
pelec->pot->get_eff_v(),
246246
pv,

source/source_io/module_hs/write_HS_R.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@
88
#include "source_lcao/spar_st.h"
99
#include "write_HS_sparse.h"
1010

11+
namespace {
12+
// Helper: Convert sparse map to HContainer
13+
template <typename T>
14+
hamilt::HContainer<T>* sparse_map_to_hcontainer(
15+
const std::map<Abfs::Vector3_Order<int>, std::map<size_t, std::map<size_t, T>>>& sparse_map,
16+
const Parallel_Orbitals& pv,
17+
const int nbasis)
18+
{
19+
hamilt::HContainer<T>* hc = new hamilt::HContainer<T>(&pv);
20+
hc->set_zero();
21+
22+
for (const auto& [R, row_map] : sparse_map)
23+
{
24+
for (const auto& [row, col_map] : row_map)
25+
{
26+
for (const auto& [col, value] : col_map)
27+
{
28+
hc->set_value(R.x, R.y, R.z, row, col, value);
29+
}
30+
}
31+
}
32+
33+
return hc;
34+
}
35+
} // anonymous namespace
36+
1137
// if 'binary=true', output binary file.
1238
// The 'sparse_thr' is the accuracy of the sparse matrix.
1339
// If the absolute value of the matrix element is less than or equal to the
@@ -226,6 +252,20 @@ std::string ModuleIO::hsr_gen_fname(const std::string& prefix,
226252
}
227253
}
228254

255+
std::string ModuleIO::dhr_gen_fname(const std::string& prefix,
256+
const int ispin,
257+
const bool append,
258+
const int istep)
259+
{
260+
std::string fname = prefix + "rs" + std::to_string(ispin + 1);
261+
if (!append && istep >= 0)
262+
{
263+
fname += "g" + std::to_string(istep + 1);
264+
}
265+
fname += "_nao.csr";
266+
return fname;
267+
}
268+
229269
template <typename TR>
230270
void ModuleIO::write_hcontainer_csr(const std::string& fname,
231271
const UnitCell* ucell,
@@ -344,3 +384,78 @@ template void ModuleIO::write_hsr<std::complex<double>>(
344384
const UnitCell*, const int, const Parallel_2D&,
345385
const bool, const int*, const int, const int);
346386

387+
388+
template <typename TR>
389+
void ModuleIO::write_matrix_r(const std::string& matrix_label,
390+
const std::string& description,
391+
const std::vector<hamilt::HContainer<TR>*>& matrices,
392+
const UnitCell* ucell,
393+
const int precision,
394+
const Parallel_2D& paraV,
395+
const bool append,
396+
const int* iat2iwt,
397+
const int nat,
398+
const int istep)
399+
{
400+
const int nspin = matrices.size();
401+
assert(nspin > 0);
402+
403+
for (int ispin = 0; ispin < nspin; ispin++)
404+
{
405+
const int nbasis = matrices[ispin]->get_nbasis();
406+
407+
// Generate filename
408+
std::string fname = dhr_gen_fname(matrix_label, ispin, append, istep);
409+
if (PARAM.inp.calculation == "md" && !PARAM.inp.out_app_flag)
410+
{
411+
fname = PARAM.globalv.global_matrix_dir + fname;
412+
}
413+
else
414+
{
415+
fname = PARAM.globalv.global_out_dir + fname;
416+
}
417+
418+
// Gather parallel matrix to serial
419+
#ifdef __MPI
420+
Parallel_Orbitals serialV;
421+
serialV.init(nbasis, nbasis, nbasis, paraV.comm());
422+
serialV.set_serial(nbasis, nbasis);
423+
serialV.set_atomic_trace(iat2iwt, nat, nbasis);
424+
425+
hamilt::HContainer<TR> matrix_serial(&serialV);
426+
hamilt::gatherParallels(*matrices[ispin], &matrix_serial, 0);
427+
428+
if (GlobalV::MY_RANK == 0)
429+
{
430+
write_hcontainer_csr(fname, ucell, precision, &matrix_serial, istep, ispin, nspin, description);
431+
}
432+
#else
433+
write_hcontainer_csr(fname, ucell, precision, matrices[ispin], istep, ispin, nspin, description);
434+
#endif
435+
}
436+
}
437+
438+
// Template instantiations
439+
template void ModuleIO::write_matrix_r<double>(
440+
const std::string&,
441+
const std::string&,
442+
const std::vector<hamilt::HContainer<double>*>&,
443+
const UnitCell*,
444+
const int,
445+
const Parallel_2D&,
446+
const bool,
447+
const int*,
448+
const int,
449+
const int);
450+
451+
template void ModuleIO::write_matrix_r<std::complex<double>>(
452+
const std::string&,
453+
const std::string&,
454+
const std::vector<hamilt::HContainer<std::complex<double>>*>&,
455+
const UnitCell*,
456+
const int,
457+
const Parallel_2D&,
458+
const bool,
459+
const int*,
460+
const int,
461+
const int);

source/source_io/module_hs/write_HS_R.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ std::string hsr_gen_fname(const std::string& prefix,
6363
const bool append,
6464
const int istep);
6565

66+
/// Generate filename for derivative matrices (dH/dR, dS/dR).
67+
std::string dhr_gen_fname(const std::string& prefix,
68+
const int ispin,
69+
const bool append,
70+
const int istep);
71+
6672
/// Write a single HContainer to CSR file with header.
6773
template <typename TR>
6874
void write_hcontainer_csr(const std::string& fname,
@@ -86,6 +92,19 @@ void write_hsr(const std::vector<hamilt::HContainer<TR>*>& hr_vec,
8692
const int nat,
8793
const int istep);
8894

95+
/// Write real-space matrix in CSR format (generic interface).
96+
template <typename TR>
97+
void write_matrix_r(const std::string& matrix_label,
98+
const std::string& description,
99+
const std::vector<hamilt::HContainer<TR>*>& matrices,
100+
const UnitCell* ucell,
101+
const int precision,
102+
const Parallel_2D& paraV,
103+
const bool append,
104+
const int* iat2iwt,
105+
const int nat,
106+
const int istep);
107+
89108
} // namespace ModuleIO
90109

91110
#endif

source/source_io/module_parameter/input_parameter.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,18 +387,18 @@ struct Input_para
387387
std::vector<int> out_mat_tk = {0, 8}; ///< output T(k) matrix in local basis.
388388
std::vector<int> out_mat_l = {0, 8}; ///< output L matrix in local basis.
389389
std::vector<int> out_mat_hs2 = {0, 8}; ///< output H(R) and S(R) matrix with precision
390-
bool out_mat_dh = false;
391-
bool out_mat_ds = false;
390+
std::vector<int> out_mat_dh = {0, 8}; ///< output dH/dR matrices with precision
391+
std::vector<int> out_mat_ds = {0, 8}; ///< output dS/dR matrices with precision
392392
bool out_mat_xc = false; ///< output exchange-correlation matrix in
393393
///< KS-orbital representation.
394-
bool out_mat_xc2 = false; ///< output exchange-correlation matrix Vxc(R) in NAO representation.
394+
std::vector<int> out_mat_xc2 = {0, 8}; ///< output Vxc(R) matrix with precision
395395
bool out_eband_terms = false; ///< output the band energy terms separately
396396
bool out_app_flag = true; ///< whether output r(R), H(R), S(R), T(R), and dH(R) matrices
397397
///< in an append manner during MD liuyu 2023-03-20
398398
int out_ndigits = 8; ///< Assuming 8 digits precision is needed for matrices output
399-
bool out_mat_t = false;
399+
std::vector<int> out_mat_t = {0, 8}; ///< output T(R) matrix with precision
400400
bool out_element_info = false; ///< output information of all elements
401-
bool out_mat_r = false; ///< jingan add 2019-8-14, output r(R) matrix.
401+
std::vector<int> out_mat_r = {0, 8}; ///< output r(R) matrix with precision
402402
int out_wfc_lcao = 0; ///< output the wave functions in local basis.
403403
bool out_dipole = false; ///< output the dipole or not
404404
bool out_efield = false; ///< output the efield or not

source/source_io/module_parameter/read_input.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ void ReadInput::create_directory(const Parameter& param)
253253
//----------------------------------------------------------
254254
bool out_dir = false;
255255
if (!param.input.out_app_flag
256-
&& (param.input.out_mat_hs2[0] || param.input.out_mat_r || param.input.out_mat_t || param.input.out_mat_dh || param.input.out_mat_ds))
256+
&& (param.input.out_mat_hs2[0] || param.input.out_mat_r[0] || param.input.out_mat_t[0] || param.input.out_mat_dh[0] || param.input.out_mat_ds[0]))
257257
{
258258
out_dir = true;
259259
}

0 commit comments

Comments
 (0)