|
8 | 8 | #include "source_lcao/spar_st.h" |
9 | 9 | #include "write_HS_sparse.h" |
10 | 10 |
|
| 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 | + |
11 | 37 | // if 'binary=true', output binary file. |
12 | 38 | // The 'sparse_thr' is the accuracy of the sparse matrix. |
13 | 39 | // 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, |
226 | 252 | } |
227 | 253 | } |
228 | 254 |
|
| 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 | + |
229 | 269 | template <typename TR> |
230 | 270 | void ModuleIO::write_hcontainer_csr(const std::string& fname, |
231 | 271 | const UnitCell* ucell, |
@@ -344,3 +384,78 @@ template void ModuleIO::write_hsr<std::complex<double>>( |
344 | 384 | const UnitCell*, const int, const Parallel_2D&, |
345 | 385 | const bool, const int*, const int, const int); |
346 | 386 |
|
| 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); |
0 commit comments