Skip to content

Commit f0d64c0

Browse files
author
dyzheng
committed
Fix: force error in DFT+U
1 parent 6098fba commit f0d64c0

4 files changed

Lines changed: 46 additions & 35 deletions

File tree

source/module_esolver/esolver_ks_pw.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,13 @@ void ESolver_KS_PW<T, Device>::before_scf(const int istep) {
393393
if (GlobalC::ucell.cell_parameter_updated) {
394394
this->init_after_vc(PARAM.inp, GlobalC::ucell);
395395
}
396+
397+
// if uramping > 0, and someone want to test the pbe first in each relax step, use this line below:
398+
//ModuleBase::GlobalFunc::ZEROS(GlobalC::dftu.U, GlobalC::ucell.ntype);
399+
400+
// if someone want to test relax from atomic charge but not extrapolated charge, use these two lines below rather than the if(){} block:
401+
//this->CE.update_all_dis(GlobalC::ucell);
402+
//this->sf.setup_structure_factor(&GlobalC::ucell, this->pelec->charge->rhopw);
396403
if (GlobalC::ucell.ionic_position_updated) {
397404
this->CE.update_all_dis(GlobalC::ucell);
398405
this->CE.extrapolate_charge(
@@ -601,10 +608,6 @@ void ESolver_KS_PW<T, Device>::iter_init(const int istep, const int iter) {
601608
this->p_chgmix->mixing_restart_count++;
602609
if (GlobalV::dft_plus_u)
603610
{
604-
if (GlobalC::dftu.uramping > 0.01 && !GlobalC::dftu.u_converged())
605-
{
606-
this->p_chgmix->mixing_restart_step = GlobalV::SCF_NMAX + 1;
607-
}
608611
if (GlobalC::dftu.uramping > 0.01)
609612
{
610613
bool do_uramping = true;
@@ -627,6 +630,10 @@ void ESolver_KS_PW<T, Device>::iter_init(const int istep, const int iter) {
627630
std::cout << " eV " << std::endl;
628631
}
629632
}
633+
if (GlobalC::dftu.uramping > 0.01 && !GlobalC::dftu.u_converged())
634+
{
635+
this->p_chgmix->mixing_restart_step = GlobalV::SCF_NMAX + 1;
636+
}
630637
}
631638
}
632639
// mohan move harris functional to here, 2012-06-05

source/module_hamilt_pw/hamilt_pwdft/kernels/force_op.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ struct cal_force_nl_op<FPTYPE, base_device::DEVICE_CPU>
274274
const int nproj = atom_nh[it];
275275
if(orbital_l == -1)
276276
{
277+
iat0 += atom_na[it];
277278
sum0 += nproj * atom_na[it];
278279
continue;
279280
}

source/module_hamilt_pw/hamilt_pwdft/kernels/stress_op.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ struct cal_stress_nl_op<FPTYPE, base_device::DEVICE_CPU>
227227
FPTYPE* stress)
228228
{
229229
FPTYPE local_stress = 0;
230-
int iat = 0, sum = 0;
230+
int sum = 0;
231231
for (int it = 0; it < ntype; it++)
232232
{
233233
const int orbital_l = orbital_corr[it];
@@ -294,7 +294,6 @@ struct cal_stress_nl_op<FPTYPE, base_device::DEVICE_CPU>
294294
vu += npol * npol * tlp1_2;// step for vu
295295
}// ia
296296
sum += atom_na[it] * nproj;
297-
iat += atom_na[it];
298297
} // end it
299298
*stress += local_stress;
300299
};

source/module_hamilt_pw/hamilt_pwdft/onsite_projector.cpp

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -108,23 +108,25 @@ void projectors::OnsiteProjector<T, Device>::init(const std::string& orbital_dir
108108
const ModuleBase::matrix& ekb)
109109
{
110110
this->device = base_device::get_device_type<Device>(this->ctx);
111-
if(!this->initialed)
112-
{
113-
this->ucell = ucell_in;
114-
this->ntype = ucell_in->ntype;
111+
112+
this->ucell = ucell_in;
113+
this->ntype = ucell_in->ntype;
115114

116-
this->pw_basis_ = &pw_basis;
117-
this->sf_ = &sf;
115+
this->pw_basis_ = &pw_basis;
116+
this->sf_ = &sf;
118117

119-
std::vector<std::string> orb_files(ntype);
120-
std::vector<int> nproj(ntype);
121-
int sum_nproj = 0;
122-
for(int it=0;it<ntype;++it)
123-
{
124-
orb_files[it] = ucell->orbital_fn[it];
125-
nproj[it] = ucell->atoms[it].nwl;
126-
sum_nproj += nproj[it];
127-
}
118+
std::vector<std::string> orb_files(ntype);
119+
std::vector<int> nproj(ntype);
120+
int sum_nproj = 0;
121+
for(int it=0;it<ntype;++it)
122+
{
123+
orb_files[it] = ucell->orbital_fn[it];
124+
nproj[it] = ucell->atoms[it].nwl;
125+
sum_nproj += nproj[it];
126+
}
127+
// do orbital modulate only once
128+
if(!this->initialed)
129+
{
128130
this->lproj.resize(sum_nproj);
129131
int index = 0;
130132
for(int it=0;it<ntype;++it)
@@ -157,7 +159,7 @@ void projectors::OnsiteProjector<T, Device>::init(const std::string& orbital_dir
157159
iproj,
158160
onsite_r);
159161

160-
ModuleBase::timer::tick("OnsiteProj", "cubspl_tabulate");
162+
161163
// STAGE 0 - making the interpolation table
162164
// CACHE 0 - if cache the irow2it, irow2iproj, irow2m, itiaiprojm2irow, <G+k|p> can be reused for
163165
// SCF, RELAX and CELL-RELAX calculation
@@ -166,24 +168,26 @@ void projectors::OnsiteProjector<T, Device>::init(const std::string& orbital_dir
166168
RadialProjection::RadialProjector::_build_forward_map(it2ia, it2iproj, lproj, itiaiprojm2irow_);
167169
//rp_._build_sbt_tab(rgrid, projs, lproj, nq, dq);
168170
rp_._build_sbt_tab(nproj, rgrid, projs, lproj, nq, dq, ucell_in->omega, psi.npol, tab, nhtol);
169-
// For being compatible with present cal_force and cal_stress framework
170-
// uncomment the following code block if you want to use the FS_Nonlocal_tools
171-
if(this->tab_atomic_ == nullptr)
172-
{
173-
this->tot_nproj = itiaiprojm2irow_.size();
174-
this->npwx_ = this->pw_basis_->npwk_max;
175-
this->size_vproj = this->tot_nproj * this->npwx_;
176-
resmem_complex_op()(this->ctx, this->tab_atomic_, this->size_vproj, "OnsiteP::tab_atomic_");
177-
}
178-
179-
delete this->fs_tools; // it is okay to delete nullptr
180-
this->fs_tools = new hamilt::FS_Nonlocal_tools<T, Device>(
181-
nproj, lproj, tab, nhtol, this->tab_atomic_, ucell_in, &psi, &kv, &pw_basis, &sf, wg, ekb);
182171

183172
ModuleBase::timer::tick("OnsiteProj", "cubspl_tabulate");
184173

185174
this->initialed = true;
186175
}
176+
177+
// For being compatible with present cal_force and cal_stress framework
178+
// uncomment the following code block if you want to use the FS_Nonlocal_tools
179+
ModuleBase::timer::tick("OnsiteProj", "cubspl_tabulate");
180+
{
181+
this->tot_nproj = itiaiprojm2irow_.size();
182+
this->npwx_ = this->pw_basis_->npwk_max;
183+
this->size_vproj = this->tot_nproj * this->npwx_;
184+
resmem_complex_op()(this->ctx, this->tab_atomic_, this->size_vproj, "OnsiteP::tab_atomic_");
185+
}
186+
187+
delete this->fs_tools; // it is okay to delete nullptr
188+
this->fs_tools = new hamilt::FS_Nonlocal_tools<T, Device>(
189+
nproj, lproj, tab, nhtol, this->tab_atomic_, ucell_in, &psi, &kv, &pw_basis, &sf, wg, ekb);
190+
ModuleBase::timer::tick("OnsiteProj", "cubspl_tabulate");
187191
}
188192

189193
template<typename T, typename Device>

0 commit comments

Comments
 (0)