Skip to content

Commit cfe9672

Browse files
committed
flatten the map
1 parent 2c8ced4 commit cfe9672

File tree

3 files changed

+40
-40
lines changed

3 files changed

+40
-40
lines changed

source/module_lr/potentials/pot_hxc_lrtd.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace LR
3737
const std::set<std::string> lda_xc = { "lda", "pwlda" };
3838
assert(lda_xc.count(this->xc_kernel_));
3939
const int n_component = (1 == nspin) ? 1 : 3;
40-
std::vector<double> v2rho2(n_component * nrxx);
40+
this->xc_kernel_components_.v2rho2.resize(n_component * nrxx);
4141
// read fxc adn add to xc_kernel_components
4242
assert(lr_init_xc_kernel.size() >= n_component + 1);
4343
for (int is = 0;is < n_component;++is)
@@ -47,9 +47,8 @@ namespace LR
4747
std::vector<double> v2rho2_tmp(nrxx);
4848
ModuleIO::read_vdata_palgrid(pgrid, GlobalV::MY_RANK, GlobalV::ofs_running, lr_init_xc_kernel[is + 1],
4949
v2rho2_tmp.data(), ucell->nat);
50-
for (int ir = 0;ir < nrxx;++ir) { v2rho2[ir * n_component + is] = v2rho2_tmp[ir]; }
50+
for (int ir = 0;ir < nrxx;++ir) { xc_kernel_components_.v2rho2[ir * n_component + is] = v2rho2_tmp[ir]; }
5151
}
52-
this->xc_kernel_components_.set_kernel("v2rho2", std::move(v2rho2));
5352
return;
5453
}
5554

@@ -115,7 +114,7 @@ namespace LR
115114
case SpinType::S1:
116115
funcs[s] = [this, &fxc](FXC_PARA_TYPE)->void
117116
{
118-
for (int ir = 0;ir < nrxx;++ir) { v_eff(0, ir) += ModuleBase::e2 * fxc.get_kernel("v2rho2").at(ir) * rho[ir]; }
117+
for (int ir = 0;ir < nrxx;++ir) { v_eff(0, ir) += ModuleBase::e2 * fxc.v2rho2.at(ir) * rho[ir]; }
119118
};
120119
break;
121120
case SpinType::S2_singlet:
@@ -125,7 +124,7 @@ namespace LR
125124
{
126125
const int irs0 = 3 * ir;
127126
const int irs1 = irs0 + 1;
128-
v_eff(0, ir) += ModuleBase::e2 * (fxc.get_kernel("v2rho2").at(irs0) + fxc.get_kernel("v2rho2").at(irs1)) * rho[ir];
127+
v_eff(0, ir) += ModuleBase::e2 * (fxc.v2rho2.at(irs0) + fxc.v2rho2.at(irs1)) * rho[ir];
129128
}
130129
};
131130
break;
@@ -136,7 +135,7 @@ namespace LR
136135
{
137136
const int irs0 = 3 * ir;
138137
const int irs1 = irs0 + 1;
139-
v_eff(0, ir) += ModuleBase::e2 * (fxc.get_kernel("v2rho2").at(irs0) - fxc.get_kernel("v2rho2").at(irs1)) * rho[ir];
138+
v_eff(0, ir) += ModuleBase::e2 * (fxc.v2rho2.at(irs0) - fxc.v2rho2.at(irs1)) * rho[ir];
140139
}
141140
};
142141
break;
@@ -145,7 +144,7 @@ namespace LR
145144
{
146145
assert(ispin_op.size() >= 2);
147146
const int is = ispin_op[0] + ispin_op[1];
148-
for (int ir = 0;ir < nrxx;++ir) { v_eff(0, ir) += ModuleBase::e2 * fxc.get_kernel("v2rho2").at(3 * ir + is) * rho[ir]; }
147+
for (int ir = 0;ir < nrxx;++ir) { v_eff(0, ir) += ModuleBase::e2 * fxc.v2rho2.at(3 * ir + is) * rho[ir]; }
149148
};
150149
break;
151150
default:
@@ -180,17 +179,17 @@ namespace LR
180179
std::vector<ModuleBase::Vector3<double>> e_drho(nrxx);
181180
for (int ir = 0;ir < nrxx;++ir)
182181
{
183-
e_drho[ir] = -(fxc.get_grad_kernel("2_v2rhosigma_drho").at(ir) * rho[ir]
184-
+ fxc.get_grad_kernel("4_v2sigma2_drho").at(ir) * (fxc.get_grad_kernel("drho_gs").at(ir) * drho.at(ir))
185-
+ drho.at(ir) * fxc.get_kernel("vsigma").at(ir) * 2.);
182+
e_drho[ir] = -(fxc.v2rhosigma_2drho.at(ir) * rho[ir]
183+
+ fxc.v2sigma2_4drho.at(ir) * (fxc.drho_gs.at(ir) * drho.at(ir))
184+
+ drho.at(ir) * fxc.vsigma.at(ir) * 2.);
186185
}
187186
XC_Functional::grad_dot(e_drho.data(), vxc_tmp.data(), this->rho_basis_, this->tpiba_);
188187

189188
// 2. $f^{\rho\rho}\rho_1+2f^{\rho\sigma}\nabla\rho\cdot\nabla\rho_1$
190189
for (int ir = 0;ir < nrxx;++ir)
191190
{
192-
vxc_tmp[ir] += (fxc.get_kernel("v2rho2").at(ir) * rho[ir]
193-
+ fxc.get_grad_kernel("2_v2rhosigma_drho").at(ir) * drho.at(ir));
191+
vxc_tmp[ir] += (fxc.v2rho2.at(ir) * rho[ir]
192+
+ fxc.v2rhosigma_2drho.at(ir) * drho.at(ir));
194193
}
195194
BlasConnector::axpy(nrxx, ModuleBase::e2, vxc_tmp.data(), 1, v_eff.c, 1);
196195
};

source/module_lr/potentials/xc_kernel.cpp

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ void LR::KernelXC::f_xc_libxc(const int& nspin, const double& omega, const doubl
9696
}
9797
// -----------------------------------------------------------------------------------
9898
//==================== XC Kernels (f_xc)=============================
99-
this->kernel_set_.emplace("vrho", std::vector<double>(nspin * nrxx));
100-
this->kernel_set_.emplace("v2rho2", std::vector<double>(((1 == nspin) ? 1 : 3) * nrxx));//(nrxx* ((1 == nspin) ? 1 : 3)): 00, 01, 11
99+
this->vrho.resize(nspin * nrxx);
100+
this->v2rho2.resize(((1 == nspin) ? 1 : 3) * nrxx);//(nrxx* ((1 == nspin) ? 1 : 3)): 00, 01, 11
101101
if (is_gga)
102102
{
103-
this->kernel_set_.emplace("vsigma", std::vector<double>(((1 == nspin) ? 1 : 3) * nrxx)); //(nrxx*): 2 for rho * 3 for sigma: 00, 01, 02, 10, 11, 12
104-
this->kernel_set_.emplace("v2rhosigma", std::vector<double>(((1 == nspin) ? 1 : 6) * nrxx)); //(nrxx*): 2 for rho * 3 for sigma: 00, 01, 02, 10, 11, 12
105-
this->kernel_set_.emplace("v2sigma2", std::vector<double>(((1 == nspin) ? 1 : 6) * nrxx)); //(nrxx* ((1 == nspin) ? 1 : 6)): 00, 01, 02, 11, 12, 22
103+
this->vsigma.resize(((1 == nspin) ? 1 : 3) * nrxx);//(nrxx*): 2 for rho * 3 for sigma: 00, 01, 02, 10, 11, 12
104+
this->v2rhosigma.resize(((1 == nspin) ? 1 : 6) * nrxx); //(nrxx*): 2 for rho * 3 for sigma: 00, 01, 02, 10, 11, 12
105+
this->v2sigma2.resize(((1 == nspin) ? 1 : 6) * nrxx); //(nrxx* ((1 == nspin) ? 1 : 6)): 00, 01, 02, 11, 12, 22
106106
}
107107
//MetaGGA ...
108108

@@ -118,15 +118,13 @@ void LR::KernelXC::f_xc_libxc(const int& nspin, const double& omega, const doubl
118118
switch (func.info->family)
119119
{
120120
case XC_FAMILY_LDA:
121-
xc_lda_vxc(&func, nrxx, rho.data(), this->kernel_set_["vrho"].data());
122-
xc_lda_fxc(&func, nrxx, rho.data(), this->kernel_set_["v2rho2"].data());
121+
xc_lda_vxc(&func, nrxx, rho.data(), vrho.data());
122+
xc_lda_fxc(&func, nrxx, rho.data(), v2rho2.data());
123123
break;
124124
case XC_FAMILY_GGA:
125125
case XC_FAMILY_HYB_GGA:
126-
xc_gga_vxc(&func, nrxx, rho.data(), sigma.data(),
127-
this->kernel_set_["vrho"].data(), this->kernel_set_["vsigma"].data());
128-
xc_gga_fxc(&func, nrxx, rho.data(), sigma.data(),
129-
this->kernel_set_["v2rho2"].data(), this->kernel_set_["v2rhosigma"].data(), this->kernel_set_["v2sigma2"].data());
126+
xc_gga_vxc(&func, nrxx, rho.data(), sigma.data(), vrho.data(), vsigma.data());
127+
xc_gga_fxc(&func, nrxx, rho.data(), sigma.data(), v2rho2.data(), v2rhosigma.data(), v2sigma2.data());
130128
break;
131129
default:
132130
throw std::domain_error("func.info->family =" + std::to_string(func.info->family)
@@ -136,24 +134,24 @@ void LR::KernelXC::f_xc_libxc(const int& nspin, const double& omega, const doubl
136134
// some formulas for GGA
137135
if (func.info->family == XC_FAMILY_GGA || func.info->family == XC_FAMILY_HYB_GGA)
138136
{
139-
const std::vector<double>& v2r2 = this->kernel_set_["v2rho2"];
140-
const std::vector<double>& v2rs = this->kernel_set_["v2rhosigma"];
141-
const std::vector<double>& v2s2 = this->kernel_set_["v2sigma2"];
142-
const std::vector<double>& vs = this->kernel_set_["vsigma"];
137+
const std::vector<double>& v2r2 = this->v2rho2;
138+
const std::vector<double>& v2rs = this->v2rhosigma;
139+
const std::vector<double>& v2s2 = this->v2sigma2;
140+
const std::vector<double>& vs = this->vsigma;
143141
const double tpiba2 = tpiba * tpiba;
144142

145143
if (1 == nspin)
146144
{
147145
using V3 = ModuleBase::Vector3<double>;
148146
// 0. drho
149-
this->grad_kernel_set_.emplace("drho_gs", gradrho[0]);
147+
this->drho_gs = gradrho[0];
150148
// 1. $2f^{\rho\sigma}*\nabla\rho$
151-
this->grad_kernel_set_.emplace("2_v2rhosigma_drho", std::vector<V3>(nrxx));
152-
std::transform(gradrho[0].begin(), gradrho[0].end(), v2rs.begin(), this->grad_kernel_set_["2_v2rhosigma_drho"].begin(),
149+
this->v2rhosigma_2drho.resize(nrxx);
150+
std::transform(gradrho[0].begin(), gradrho[0].end(), v2rs.begin(), this->v2rhosigma_2drho.begin(),
153151
[](const V3& a, const V3& b) {return a * b * 2.; });
154152
// 2. $4f^{\sigma\sigma}*\nabla\rho$
155-
this->grad_kernel_set_.emplace("4_v2sigma2_drho", std::vector<V3>(nrxx));
156-
std::transform(sigma.begin(), sigma.end(), v2s2.begin(), this->grad_kernel_set_["4_v2sigma2_drho"].begin(),
153+
this->v2sigma2_4drho.resize(nrxx);
154+
std::transform(sigma.begin(), sigma.end(), v2s2.begin(), v2sigma2_4drho.begin(),
157155
[](const V3& a, const V3& b) {return a * b * 4.; });
158156
}
159157
// else if (2 == nspin) // wrong, to be fixed

source/module_lr/potentials/xc_kernel.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@ namespace LR
1515
void f_xc_libxc(const int& nspin, const double& omega, const double& tpiba, const double* const* const rho_gs, const double* const rho_core = nullptr);
1616
#endif
1717

18-
// Setters and getters for certain components, such as v2rho2, v2rhosigma, v2sigma2, or other custom combinations.
19-
void set_kernel(const std::string& name, const std::vector<double>& vec) { this->kernel_set_[name] = vec; }
20-
void set_kernel(const std::string& name, const std::vector<double>&& vec) { this->kernel_set_[name] = std::move(vec); }
21-
const std::vector<double>& get_kernel(const std::string& name) { return kernel_set_[name]; }
22-
const std::vector<ModuleBase::Vector3<double>>& get_grad_kernel(const std::string& name) { return grad_kernel_set_[name]; }
18+
// See https://libxc.gitlab.io/manual/libxc-5.1.x/ for the naming convention of the following members.
19+
// std::map<std::string, std::vector<double>> kernel_set_; // [kernel_type][nrxx][nspin]
20+
std::vector<double> vrho;
21+
std::vector<double> vsigma;
22+
std::vector<double> v2rho2;
23+
std::vector<double> v2rhosigma;
24+
std::vector<double> v2sigma2;
25+
// std::map<std::string, std::vector<ModuleBase::Vector3<double>>> grad_kernel_set_;// [kernel_type][nrxx][nspin], intermediate terms for GGA
26+
std::vector<ModuleBase::Vector3<double>> drho_gs;
27+
std::vector<ModuleBase::Vector3<double>> v2rhosigma_2drho; ///< $f^{\rho\sigma}*\nabla\rho *2$
28+
std::vector<ModuleBase::Vector3<double>> v2sigma2_4drho; ///< $f^{\sigma\sigma}*\nabla\rho *4$
2329

2430
protected:
25-
2631
const ModulePW::PW_Basis& rho_basis_;
27-
std::map<std::string, std::vector<double>> kernel_set_; // [kernel_type][nrxx][nspin]
28-
std::map<std::string, std::vector<ModuleBase::Vector3<double>>> grad_kernel_set_;// [kernel_type][nrxx][nspin], intermediate terms for GGA
2932
};
3033
}
3134

0 commit comments

Comments
 (0)