Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: remove some variables from LCAO_Matrix class #4568

Merged
merged 15 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source/Makefile.Objects
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ OBJS_LCAO=DM_gamma.o\
LCAO_nl_beta.o\
LCAO_nnr.o\
LCAO_set_zero.o\
LCAO_allocate.o\
center2_orb.o\
center2_orb-orb11.o\
center2_orb-orb21.o\
Expand Down
56 changes: 42 additions & 14 deletions source/module_base/formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class FmtCore
static inline std::string format(const char* fmt, const Ts&... args)
{
const int size = snprintf(nullptr, 0, fmt, FmtCore::filter(args)...) + 1;
std::string dst(size, '\0');
snprintf(&dst[0], size, fmt, FmtCore::filter(args)...);
dst.pop_back();
std::string dst(size, ' ');
const int size_filled = snprintf(&dst[0], size, fmt, FmtCore::filter(args)...);
dst.resize(size_filled);
return dst;
}
/**
Expand Down Expand Up @@ -120,7 +120,9 @@ class FmtCore
}
static std::string center(const std::string& in, const size_t& width, const char& fillchar = ' ')
{
if(in.size() >= width) return in;
if (in.size() >= width) {
return in;
}
const size_t nwhitespaces = width - in.size();
const size_t nleft = nwhitespaces / 2;
const size_t nright = nwhitespaces - nleft;
Expand Down Expand Up @@ -264,7 +266,9 @@ class FmtTable
for(size_t i = 0; i < titles.size(); i++)
{
dst += titles[i];
if(i != titles.size() - 1) dst += delimiters_.v_;
if (i != titles.size() - 1) {
dst += delimiters_.v_;
}
}
dst += std::string(1, frames_.r_) + "\n" + std::string(width, frames_.mid_) + "\n";
return dst;
Expand All @@ -285,15 +289,21 @@ class FmtTable
width += row.size() - 1;
// for the left and right frame
width += 2;
if(pos == 't') dst += std::string(width, frames_.up_) + "\n";
if (pos == 't') {
dst += std::string(width, frames_.up_) + "\n";
}
dst += std::string(1, frames_.l_);
for(size_t i = 0; i < row.size(); i++)
{
dst += row[i];
if(i != row.size() - 1) dst += delimiters_.v_;
if (i != row.size() - 1) {
dst += delimiters_.v_;
}
}
dst += std::string(1, frames_.r_) + "\n";
if(pos == 'b') dst += std::string(width, frames_.dw_) + "\n";
if (pos == 'b') {
dst += std::string(width, frames_.dw_) + "\n";
}
return dst;
}
/**
Expand All @@ -308,24 +318,35 @@ class FmtTable
const size_t ncols = data_.shape()[1];
// if not all titles are empty, then with_title boolean will be true
bool with_title = false;
for(auto& title : titles_) if(!title.empty()) { with_title = true; break; }
for (auto& title: titles_) {
if (!title.empty()) {
with_title = true;
break;
}
}
// first to relax each column
for(size_t j = 0UL; j < ncols; j++)
{
std::vector<std::string> col(nrows);
for(size_t i = 0UL; i < nrows; i++) col[i] = data_(i, j);
for (size_t i = 0UL; i < nrows; i++) {
col[i] = data_(i, j);
}
col = relax_col_width(col, titles_[j], aligns_.val_, aligns_.title_);
titles_[j] = col[0UL];
std::vector<std::string> col_new(col.begin() + 1, col.end());
set_value(0UL, j, 'v', col_new);
}
// then print titles
if(with_title) dst += concat_title(titles_);
if (with_title) {
dst += concat_title(titles_);
}
// then print contents
for(size_t i = 0UL; i < nrows; i++)
{
std::vector<std::string> row(ncols);
for(size_t j = 0; j < ncols; j++) row[j] = data_(i, j);
for (size_t j = 0; j < ncols; j++) {
row[j] = data_(i, j);
}
dst += concat_row(row, ((i == 0UL)&&!with_title)? 't': (i == nrows - 1)? 'b': 'n');
}
return dst;
Expand All @@ -346,8 +367,15 @@ class FmtTable
template<typename T>
void set_value(const size_t& i, const size_t& j, const char& dir, const std::vector<T>& src)
{
if(dir == 'v') for(size_t k = 0UL; k < src.size(); k++) data_(i + k, j) = src[k];
else if(dir == 'h') for(size_t k = 0UL; k < src.size(); k++) data_(j, i + k) = src[k];
if (dir == 'v') {
for (size_t k = 0UL; k < src.size(); k++) {
data_(i + k, j) = src[k];
}
} else if (dir == 'h') {
for (size_t k = 0UL; k < src.size(); k++) {
data_(j, i + k) = src[k];
}
}
}
// iterator support indices
size_t j_ = 0;
Expand Down
3 changes: 2 additions & 1 deletion source/module_esolver/esolver_ks_lcao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "module_hamilt_lcao/module_dftu/dftu.h"
#include "module_hamilt_pw/hamilt_pwdft/global.h"
#include "module_io/print_info.h"
#include "module_hamilt_lcao/hamilt_lcaodft/LCAO_domain.h" // need divide_HS_in_frag

#include <memory>
#ifdef __EXX
Expand Down Expand Up @@ -176,7 +177,7 @@ void ESolver_KS_LCAO<TK, TR>::before_all_runners(Input& inp, UnitCell& ucell) {
// 6) initialize Hamilt in LCAO
// * allocate H and S matrices according to computational resources
// * set the 'trace' between local H/S and global H/S
this->LM.divide_HS_in_frag(GlobalV::GAMMA_ONLY_LOCAL,
LCAO_domain::divide_HS_in_frag(this->LM, GlobalV::GAMMA_ONLY_LOCAL,
orb_con.ParaV,
this->kv.get_nks());

Expand Down
3 changes: 2 additions & 1 deletion source/module_esolver/esolver_ks_lcao_tddft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "module_hamilt_lcao/module_tddft/td_velocity.h"
#include "module_hamilt_pw/hamilt_pwdft/global.h"
#include "module_io/print_info.h"
#include "module_hamilt_lcao/hamilt_lcaodft/LCAO_domain.h" // need divide_HS_in_frag

//-----HSolver ElecState Hamilt--------
#include "module_elecstate/elecstate_lcao.h"
Expand Down Expand Up @@ -77,7 +78,7 @@ void ESolver_KS_LCAO_TDDFT::before_all_runners(Input& inp, UnitCell& ucell) {
this->init_basis_lcao(this->orb_con, inp, ucell);

// 5) allocate H and S matrices according to computational resources
this->LM.divide_HS_in_frag(GlobalV::GAMMA_ONLY_LOCAL,
LCAO_domain::divide_HS_in_frag(this->LM, GlobalV::GAMMA_ONLY_LOCAL,
orb_con.ParaV,
kv.get_nks());

Expand Down
1 change: 1 addition & 0 deletions source/module_hamilt_lcao/hamilt_lcaodft/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ if(ENABLE_LCAO)
LCAO_nl_mu.cpp
LCAO_nl_beta.cpp
LCAO_set_zero.cpp
LCAO_allocate.cpp
record_adj.cpp
center2_orb.cpp
center2_orb-orb11.cpp
Expand Down
98 changes: 98 additions & 0 deletions source/module_hamilt_lcao/hamilt_lcaodft/LCAO_allocate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include "module_hamilt_lcao/hamilt_lcaodft/LCAO_domain.h"
#include "module_base/timer.h"
#include "module_hamilt_pw/hamilt_pwdft/global.h"
#include "module_hamilt_lcao/module_deepks/LCAO_deepks.h"

namespace LCAO_domain
{

void divide_HS_in_frag(LCAO_Matrix& lm, const bool isGamma, Parallel_Orbitals& pv,const int& nks) {
ModuleBase::TITLE("LCAO_Matrix", "divide_HS_in_frag");

//(1), (2): set up matrix division have been moved into ORB_control
// just pass `ParaV` as pointer is enough
lm.ParaV = &pv;
// (3) allocate for S, H_fixed, H, and S_diag
if (isGamma) {
LCAO_domain::allocate_HS_gamma(lm, lm.ParaV->nloc);
} else {
LCAO_domain::allocate_HS_k(lm, lm.ParaV->nloc);
}
#ifdef __DEEPKS
// wenfei 2021-12-19
// preparation for DeePKS

if (GlobalV::deepks_out_labels || GlobalV::deepks_scf) {
// allocate relevant data structures for calculating descriptors
std::vector<int> na;
na.resize(GlobalC::ucell.ntype);
for (int it = 0; it < GlobalC::ucell.ntype; it++) {
na[it] = GlobalC::ucell.atoms[it].na;
}

GlobalC::ld.init(GlobalC::ORB,
GlobalC::ucell.nat,
GlobalC::ucell.ntype,
pv,
na);

if (GlobalV::deepks_scf) {
if (isGamma) {
GlobalC::ld.allocate_V_delta(GlobalC::ucell.nat);
} else {
GlobalC::ld.allocate_V_delta(GlobalC::ucell.nat, nks);
}
}
}
#endif
return;
}

void allocate_HS_gamma(LCAO_Matrix& lm, const long& nloc) {
ModuleBase::TITLE("LCAO_Matrix", "allocate_HS_gamma");

ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "nloc", nloc);

if (nloc == 0) {
return;
}

// because we initilize in the constructor function
// with dimension '1', so here we reconstruct these
// matrices

lm.Sloc.resize(nloc);
lm.Hloc_fixed.resize(nloc);
lm.Hloc.resize(nloc);

ModuleBase::GlobalFunc::ZEROS(lm.Sloc.data(), nloc);
ModuleBase::GlobalFunc::ZEROS(lm.Hloc_fixed.data(), nloc);
ModuleBase::GlobalFunc::ZEROS(lm.Hloc.data(), nloc);

return;
}

void allocate_HS_k(LCAO_Matrix& lm, const long& nloc) {
ModuleBase::TITLE("LCAO_Matrix", "allocate_HS_k");

ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "nloc", nloc);

if (nloc == 0) {
return; // mohan fix bug 2012-05-25
}

// because we initilize in the constructor function
// with dimension '1', so here we reconstruct these
// matrices
lm.Sloc2.resize(nloc);
lm.Hloc_fixed2.resize(nloc);
lm.Hloc2.resize(nloc);

ModuleBase::GlobalFunc::ZEROS(lm.Sloc2.data(), nloc);
ModuleBase::GlobalFunc::ZEROS(lm.Hloc_fixed2.data(), nloc);
ModuleBase::GlobalFunc::ZEROS(lm.Hloc2.data(), nloc);

return;
}

}
7 changes: 7 additions & 0 deletions source/module_hamilt_lcao/hamilt_lcaodft/LCAO_domain.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ void build_ST_new(LCAO_Matrix& lm,
* @brief set zeros for HSR matrices
*/
void zeros_HSR(const char& mtype, LCAO_HS_Arrays& HS_arrays);

void divide_HS_in_frag(LCAO_Matrix& lm, const bool isGamma, Parallel_Orbitals& pv, const int& nks);

void allocate_HS_k(LCAO_Matrix& lm, const long& nloc);

void allocate_HS_gamma(LCAO_Matrix& lm, const long& nloc);

} // namespace LCAO_domain

#endif
Loading
Loading