Skip to content

Commit 4a1750a

Browse files
Refactor:Remove GlobalC::ucell in module_dftu (#5655)
* update ucell in force_gamma/force_k * change GlobalC::ucell in the force_stress * change GlobalC::ucell in the grid_init.cpp * update ucell in hamilt_lcao * update ucell in LCAO_alloacte * update ucell in record_adj * update ucell in spra_dh.cpp * update ucell in wavefunc_in_pw * fix bug in wavefunc * change GlobalC::ucell in dftu.cpp * update ucell in dftu_force * update ucell in dftu_force * change ucell in dftu_occup * change ucell in dftu_yukawa * change ucell in dftu_io * [pre-commit.ci lite] apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 5f1b128 commit 4a1750a

File tree

10 files changed

+233
-183
lines changed

10 files changed

+233
-183
lines changed

source/module_esolver/esolver_ks_lcao.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ void ESolver_KS_LCAO<TK, TR>::iter_init(UnitCell& ucell, const int istep, const
644644
GlobalC::dftu.set_dmr(dynamic_cast<elecstate::ElecStateLCAO<TK>*>(this->pelec)->get_DM());
645645
}
646646
// Calculate U and J if Yukawa potential is used
647-
GlobalC::dftu.cal_slater_UJ(this->pelec->charge->rho, this->pw_rho->nrxx);
647+
GlobalC::dftu.cal_slater_UJ(ucell,this->pelec->charge->rho, this->pw_rho->nrxx);
648648
}
649649

650650
#ifdef __DEEPKS
@@ -875,11 +875,11 @@ void ESolver_KS_LCAO<TK, TR>::iter_finish(UnitCell& ucell, const int istep, int&
875875
{
876876
const std::vector<std::vector<TK>>& tmp_dm
877877
= dynamic_cast<elecstate::ElecStateLCAO<TK>*>(this->pelec)->get_DM()->get_DMK_vector();
878-
ModuleDFTU::dftu_cal_occup_m(iter, tmp_dm, this->kv, this->p_chgmix->get_mixing_beta(), this->p_hamilt);
878+
ModuleDFTU::dftu_cal_occup_m(iter, ucell,tmp_dm, this->kv, this->p_chgmix->get_mixing_beta(), this->p_hamilt);
879879
}
880-
GlobalC::dftu.cal_energy_correction(istep);
880+
GlobalC::dftu.cal_energy_correction(ucell,istep);
881881
}
882-
GlobalC::dftu.output();
882+
GlobalC::dftu.output(ucell);
883883
}
884884

885885
// (7) for deepks, calculate delta_e

source/module_hamilt_lcao/hamilt_lcaodft/FORCE_STRESS.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,8 @@ void Force_Stress_LCAO<T>::getForceStress(const bool isforce,
300300
}
301301
if (PARAM.inp.dft_plus_u == 2)
302302
{
303-
GlobalC::dftu.force_stress(pelec,
303+
GlobalC::dftu.force_stress(ucell,
304+
pelec,
304305
pv,
305306
fsr, // mohan 2024-06-16
306307
force_dftu,

source/module_hamilt_lcao/hamilt_lcaodft/wavefunc_in_pw.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,7 @@ void Wavefunc_in_pw::produce_local_basis_in_pw(const UnitCell& ucell,
304304
/* for(int is_N = 0; is_N < 2; is_N++)*/ //for rotate base
305305
for(int is_N = 0; is_N < 1; is_N++)
306306
{
307-
if(L==0 && is_N==1) { continue;
308-
}
307+
if(L==0 && is_N==1) { continue;}
309308
if(ucell.atoms[it].ncpp.has_so)
310309
{
311310
const double j = std::abs(double(L+is_N) - 0.5);
@@ -370,9 +369,11 @@ void Wavefunc_in_pw::produce_local_basis_in_pw(const UnitCell& ucell,
370369
for(int m = 0;m<2*L+1;m++)
371370
{
372371
const int lm = L*L +m;
373-
if (iwall + 2 * L + 1 > ucell.natomwfc) {
372+
373+
if (iwall + 2 * L + 1 > ucell.natomwfc)
374+
{
374375
ModuleBase::WARNING_QUIT("this->wf.atomic_wfc()", "error: too many wfcs");
375-
}
376+
}
376377
for (int ig = 0; ig < npw; ig++)
377378
{
378379
aux[ig] = sk[ig] * ylm(lm,ig) * chiaux[ig];

source/module_hamilt_lcao/module_dftu/dftu.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -179,37 +179,38 @@ void DFTU::init(UnitCell& cell, // unitcell class
179179
{
180180
std::stringstream sst;
181181
sst << "initial_onsite.dm";
182-
this->read_occup_m(sst.str());
182+
this->read_occup_m(cell,sst.str());
183183
#ifdef __MPI
184-
this->local_occup_bcast();
184+
this->local_occup_bcast(cell);
185185
#endif
186186

187187
initialed_locale = true;
188-
this->copy_locale();
188+
this->copy_locale(cell);
189189
}
190190
else
191191
{
192192
if (PARAM.inp.init_chg == "file")
193193
{
194194
std::stringstream sst;
195195
sst << PARAM.globalv.global_out_dir << "onsite.dm";
196-
this->read_occup_m(sst.str());
196+
this->read_occup_m(cell,sst.str());
197197
#ifdef __MPI
198-
this->local_occup_bcast();
198+
this->local_occup_bcast(cell);
199199
#endif
200200
initialed_locale = true;
201201
}
202202
else
203203
{
204-
this->zero_locale();
204+
this->zero_locale(cell);
205205
}
206206
}
207207

208208
ModuleBase::Memory::record("DFTU::locale", sizeof(double) * num_locale);
209209
return;
210210
}
211211

212-
void DFTU::cal_energy_correction(const int istep)
212+
void DFTU::cal_energy_correction(const UnitCell& ucell,
213+
const int istep)
213214
{
214215
ModuleBase::TITLE("DFTU", "cal_energy_correction");
215216
ModuleBase::timer::tick("DFTU", "cal_energy_correction");
@@ -221,18 +222,18 @@ void DFTU::cal_energy_correction(const int istep)
221222
this->EU = 0.0;
222223
double EU_dc = 0.0;
223224

224-
for (int T = 0; T < GlobalC::ucell.ntype; T++)
225+
for (int T = 0; T < ucell.ntype; T++)
225226
{
226-
const int NL = GlobalC::ucell.atoms[T].nwl + 1;
227+
const int NL = ucell.atoms[T].nwl + 1;
227228
const int LC = orbital_corr[T];
228-
for (int I = 0; I < GlobalC::ucell.atoms[T].na; I++)
229+
for (int I = 0; I < ucell.atoms[T].na; I++)
229230
{
230231
if (LC == -1)
231232
{
232233
continue;
233234
}
234235

235-
const int iat = GlobalC::ucell.itia2iat(T, I);
236+
const int iat = ucell.itia2iat(T, I);
236237
const int L = orbital_corr[T];
237238

238239
for (int l = 0; l < NL; l++)
@@ -242,7 +243,7 @@ void DFTU::cal_energy_correction(const int istep)
242243
continue;
243244
}
244245

245-
const int N = GlobalC::ucell.atoms[T].l_nchi[l];
246+
const int N = ucell.atoms[T].l_nchi[l];
246247

247248
const int m_tot = 2 * l + 1;
248249

@@ -422,22 +423,24 @@ const hamilt::HContainer<double>* DFTU::get_dmr(int ispin) const
422423
//! dftu occupation matrix for gamma only using dm(double)
423424
template <>
424425
void dftu_cal_occup_m(const int iter,
426+
const UnitCell& ucell,
425427
const std::vector<std::vector<double>>& dm,
426428
const K_Vectors& kv,
427429
const double& mixing_beta,
428430
hamilt::Hamilt<double>* p_ham)
429431
{
430-
GlobalC::dftu.cal_occup_m_gamma(iter, dm, mixing_beta, p_ham);
432+
GlobalC::dftu.cal_occup_m_gamma(iter, ucell ,dm, mixing_beta, p_ham);
431433
}
432434

433435
//! dftu occupation matrix for multiple k-points using dm(complex)
434436
template <>
435437
void dftu_cal_occup_m(const int iter,
438+
const UnitCell& ucell,
436439
const std::vector<std::vector<std::complex<double>>>& dm,
437440
const K_Vectors& kv,
438441
const double& mixing_beta,
439442
hamilt::Hamilt<std::complex<double>>* p_ham)
440443
{
441-
GlobalC::dftu.cal_occup_m_k(iter, dm, kv, mixing_beta, p_ham);
444+
GlobalC::dftu.cal_occup_m_k(iter,ucell, dm, kv, mixing_beta, p_ham);
442445
}
443446
} // namespace ModuleDFTU

source/module_hamilt_lcao/module_dftu/dftu.h

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class DFTU
4545
);
4646

4747
// calculate the energy correction
48-
void cal_energy_correction(const int istep);
48+
void cal_energy_correction(const UnitCell& ucell, const int istep);
4949
double get_energy(){return EU;}
5050
void uramping_update(); // update U by uramping
5151
bool u_converged(); // check if U is converged
@@ -89,16 +89,25 @@ class DFTU
8989
//=============================================================
9090
public:
9191
// calculate the local occupation number matrix
92-
void cal_occup_m_k(const int iter, const std::vector<std::vector<std::complex<double>>>& dm_k, const K_Vectors& kv, const double& mixing_beta, hamilt::Hamilt<std::complex<double>>* p_ham);
93-
void cal_occup_m_gamma(const int iter, const std::vector<std::vector<double>>& dm_gamma, const double& mixing_beta, hamilt::Hamilt<double>* p_ham);
92+
void cal_occup_m_k(const int iter,
93+
const UnitCell& ucell,
94+
const std::vector<std::vector<std::complex<double>>>& dm_k,
95+
const K_Vectors& kv,
96+
const double& mixing_beta,
97+
hamilt::Hamilt<std::complex<double>>* p_ham);
98+
void cal_occup_m_gamma(const int iter,
99+
const UnitCell& ucell,
100+
const std::vector<std::vector<double>>& dm_gamma,
101+
const double& mixing_beta,
102+
hamilt::Hamilt<double>* p_ham);
94103

95104
// dftu can be calculated only after locale has been initialed
96105
bool initialed_locale = false;
97106

98107
private:
99-
void copy_locale();
100-
void zero_locale();
101-
void mix_locale(const double& mixing_beta);
108+
void copy_locale(const UnitCell& ucell);
109+
void zero_locale(const UnitCell& ucell);
110+
void mix_locale(const UnitCell& ucell,const double& mixing_beta);
102111

103112
public:
104113
// local occupancy matrix of the correlated subspace
@@ -147,13 +156,14 @@ class DFTU
147156
// dim = 4-6 : dS * dR, for stress
148157

149158
void folding_matrix_k(
159+
const UnitCell &ucell,
150160
ForceStressArrays &fsr,
151161
const Parallel_Orbitals &pv,
152-
const int ik,
153-
const int dim1,
154-
const int dim2,
155-
std::complex<double>* mat_k,
156-
const std::vector<ModuleBase::Vector3<double>> &kvec_d);
162+
const int ik,
163+
const int dim1,
164+
const int dim2,
165+
std::complex<double>* mat_k,
166+
const std::vector<ModuleBase::Vector3<double>> &kvec_d);
157167

158168

159169
/**
@@ -169,38 +179,40 @@ class DFTU
169179
//=============================================================
170180
public:
171181

172-
void force_stress(const elecstate::ElecState* pelec,
173-
const Parallel_Orbitals& pv,
174-
ForceStressArrays& fsr,
175-
ModuleBase::matrix& force_dftu,
176-
ModuleBase::matrix& stress_dftu,
177-
const K_Vectors& kv);
182+
void force_stress(const UnitCell& ucell,
183+
const elecstate::ElecState* pelec,
184+
const Parallel_Orbitals& pv,
185+
ForceStressArrays& fsr,
186+
ModuleBase::matrix& force_dftu,
187+
ModuleBase::matrix& stress_dftu,
188+
const K_Vectors& kv);
178189

179190
private:
180191

181-
void cal_force_k(
182-
ForceStressArrays &fsr,
183-
const Parallel_Orbitals &pv,
184-
const int ik,
185-
const std::complex<double>* rho_VU,
186-
ModuleBase::matrix& force_dftu,
187-
const std::vector<ModuleBase::Vector3<double>>& kvec_d);
192+
void cal_force_k(const UnitCell &ucell,
193+
ForceStressArrays &fsr,
194+
const Parallel_Orbitals &pv,
195+
const int ik,
196+
const std::complex<double>* rho_VU,
197+
ModuleBase::matrix& force_dftu,
198+
const std::vector<ModuleBase::Vector3<double>>& kvec_d);
188199

189200
void cal_stress_k(
201+
const UnitCell &ucell,
190202
ForceStressArrays &fsr,
191203
const Parallel_Orbitals &pv,
192204
const int ik,
193205
const std::complex<double>* rho_VU,
194206
ModuleBase::matrix& stress_dftu,
195207
const std::vector<ModuleBase::Vector3<double>>& kvec_d);
196208

197-
void cal_force_gamma(
198-
const double* rho_VU,
199-
const Parallel_Orbitals &pv,
200-
double* dsloc_x,
201-
double* dsloc_y,
202-
double* dsloc_z,
203-
ModuleBase::matrix& force_dftu);
209+
void cal_force_gamma(const UnitCell &ucell,
210+
const double* rho_VU,
211+
const Parallel_Orbitals &pv,
212+
double* dsloc_x,
213+
double* dsloc_y,
214+
double* dsloc_z,
215+
ModuleBase::matrix& force_dftu);
204216

205217
void cal_stress_gamma(
206218
const UnitCell &ucell,
@@ -218,12 +230,15 @@ class DFTU
218230
// For reading/writing/broadcasting/copying relevant data structures
219231
//=============================================================
220232
public:
221-
void output();
233+
void output(const UnitCell& ucell);
222234

223235
private:
224-
void write_occup_m(std::ofstream& ofs, bool diag=false);
225-
void read_occup_m(const std::string& fn);
226-
void local_occup_bcast();
236+
void write_occup_m(const UnitCell& ucell,
237+
std::ofstream& ofs,
238+
bool diag=false);
239+
void read_occup_m(const UnitCell& ucell,
240+
const std::string& fn);
241+
void local_occup_bcast(const UnitCell& ucell);
227242

228243
//=============================================================
229244
// In dftu_yukawa.cpp
@@ -232,15 +247,15 @@ class DFTU
232247

233248
public:
234249
bool Yukawa; // 1:use Yukawa potential; 0: do not use Yukawa potential
235-
void cal_slater_UJ(double** rho, const int& nrxx);
250+
void cal_slater_UJ(const UnitCell& ucell, double** rho, const int& nrxx);
236251

237252
private:
238253
double lambda; // the parameter in Yukawa potential
239254
std::vector<std::vector<std::vector<std::vector<double>>>> Fk; // slater integral:Fk[T][L][N][k]
240255
std::vector<std::vector<std::vector<double>>> U_Yukawa; // U_Yukawa[T][L][N]
241256
std::vector<std::vector<std::vector<double>>> J_Yukawa; // J_Yukawa[T][L][N]
242257

243-
void cal_slater_Fk(const int L, const int T); // L:angular momnet, T:atom type
258+
void cal_slater_Fk(const UnitCell& ucell,const int L, const int T); // L:angular momnet, T:atom type
244259
void cal_yukawa_lambda(double** rho, const int& nrxx);
245260

246261
double spherical_Bessel(const int k, const double r, const double lambda);
@@ -267,6 +282,7 @@ class DFTU
267282

268283
template <typename T>
269284
void dftu_cal_occup_m(const int iter,
285+
const UnitCell& ucell,
270286
const std::vector<std::vector<T>>& dm,
271287
const K_Vectors& kv,
272288
const double& mixing_beta,

source/module_hamilt_lcao/module_dftu/dftu_folding.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ void DFTU::fold_dSR_gamma(
126126
}
127127

128128
void DFTU::folding_matrix_k(
129+
const UnitCell &ucell,
129130
ForceStressArrays &fsr,
130131
const Parallel_Orbitals &pv,
131132
const int ik,
@@ -153,26 +154,26 @@ void DFTU::folding_matrix_k(
153154
ModuleBase::Vector3<double> dtau2;
154155
ModuleBase::Vector3<double> tau0;
155156

156-
for (int T1 = 0; T1 < GlobalC::ucell.ntype; ++T1)
157+
for (int T1 = 0; T1 < ucell.ntype; ++T1)
157158
{
158-
Atom* atom1 = &GlobalC::ucell.atoms[T1];
159+
Atom* atom1 = &ucell.atoms[T1];
159160
for (int I1 = 0; I1 < atom1->na; ++I1)
160161
{
161162
tau1 = atom1->tau[I1];
162-
GlobalC::GridD.Find_atom(GlobalC::ucell, tau1, T1, I1);
163-
Atom* atom1 = &GlobalC::ucell.atoms[T1];
164-
const int start1 = GlobalC::ucell.itiaiw2iwt(T1, I1, 0);
163+
GlobalC::GridD.Find_atom(ucell, tau1, T1, I1);
164+
Atom* atom1 = &ucell.atoms[T1];
165+
const int start1 = ucell.itiaiw2iwt(T1, I1, 0);
165166

166167
// (2) search among all adjacent atoms.
167168
for (int ad = 0; ad < GlobalC::GridD.getAdjacentNum() + 1; ++ad)
168169
{
169170
const int T2 = GlobalC::GridD.getType(ad);
170171
const int I2 = GlobalC::GridD.getNatom(ad);
171-
Atom* atom2 = &GlobalC::ucell.atoms[T2];
172+
Atom* atom2 = &ucell.atoms[T2];
172173

173174
tau2 = GlobalC::GridD.getAdjacentTau(ad);
174175
dtau = tau2 - tau1;
175-
double distance = dtau.norm() * GlobalC::ucell.lat0;
176+
double distance = dtau.norm() * ucell.lat0;
176177
double rcut = orb_cutoff_[T1] + orb_cutoff_[T2];
177178

178179
bool adj = false;
@@ -192,11 +193,11 @@ void DFTU::folding_matrix_k(
192193
dtau1 = tau0 - tau1;
193194
dtau2 = tau0 - tau2;
194195

195-
double distance1 = dtau1.norm() * GlobalC::ucell.lat0;
196-
double distance2 = dtau2.norm() * GlobalC::ucell.lat0;
196+
double distance1 = dtau1.norm() * ucell.lat0;
197+
double distance2 = dtau2.norm() * ucell.lat0;
197198

198-
double rcut1 = orb_cutoff_[T1] + GlobalC::ucell.infoNL.Beta[T0].get_rcut_max();
199-
double rcut2 = orb_cutoff_[T2] + GlobalC::ucell.infoNL.Beta[T0].get_rcut_max();
199+
double rcut1 = orb_cutoff_[T1] + ucell.infoNL.Beta[T0].get_rcut_max();
200+
double rcut2 = orb_cutoff_[T2] + ucell.infoNL.Beta[T0].get_rcut_max();
200201

201202
if (distance1 < rcut1 && distance2 < rcut2)
202203
{
@@ -209,7 +210,7 @@ void DFTU::folding_matrix_k(
209210
if (adj)
210211
{
211212
// (3) calculate the nu of atom (T2, I2)
212-
const int start2 = GlobalC::ucell.itiaiw2iwt(T2, I2, 0);
213+
const int start2 = ucell.itiaiw2iwt(T2, I2, 0);
213214
//------------------------------------------------
214215
// exp(k dot dR)
215216
// dR is the index of box in Crystal coordinates

0 commit comments

Comments
 (0)