11#include " esolver_ks_pw.h"
22
3- #include " source_base/global_variable.h"
4- #include " source_base/kernels/math_kernel_op.h"
5- #include " source_base/memory.h"
63#include " source_estate/cal_ux.h"
74#include " source_estate/elecstate_pw.h"
85#include " source_estate/elecstate_pw_sdft.h"
96#include " source_estate/elecstate_tools.h"
107#include " source_estate/module_charge/symmetry_rho.h"
11- // #include "source_hamilt/module_ewald/H_Ewald_pw.h"
12- // #include "source_hamilt/module_vdw/vdw.h"
8+
139#include " source_hsolver/diago_iter_assist.h"
1410#include " source_hsolver/hsolver_pw.h"
15- #include " source_hsolver/kernels/dngvd_op.h"
1611#include " source_io/module_parameter/parameter.h"
1712#include " source_lcao/module_deltaspin/spin_constrain.h"
1813#include " source_pw/module_pwdft/onsite_projector.h"
2318#include " source_pw/module_pwdft/forces.h"
2419#include " source_pw/module_pwdft/stress_pw.h"
2520
26- // #include <iostream>
27-
28- #include < ATen/kernels/blas.h>
29- #include < ATen/kernels/lapack.h>
21+ // #include "source_base/global_variable.h"
22+ // #include "source_base/kernels/math_kernel_op.h"
23+ // #include "source_base/memory.h"
24+ // #include "source_hsolver/kernels/dngvd_op.h"
25+ // #include <ATen/kernels/blas.h>
26+ // #include <ATen/kernels/lapack.h>
3027
3128#ifdef __DSP
3229#include " source_base/kernels/dsp/dsp_connector.h"
@@ -97,10 +94,10 @@ void ESolver_KS_PW<T, Device>::deallocate_hamilt()
9794template <typename T, typename Device>
9895void ESolver_KS_PW<T, Device>::before_all_runners(UnitCell& ucell, const Input_para& inp)
9996{
100- // 1) call before_all_runners() of ESolver_KS
97+ // ! Call before_all_runners() of ESolver_KS
10198 ESolver_KS<T, Device>::before_all_runners (ucell, inp);
10299
103- // 2) initialize ElecState, set pelec pointer
100+ // ! Initialize ElecState, set pelec pointer
104101 if (this ->pelec == nullptr )
105102 {
106103 if (inp.esolver_type == " sdft" )
@@ -118,46 +115,40 @@ void ESolver_KS_PW<T, Device>::before_all_runners(UnitCell& ucell, const Input_p
118115 }
119116 }
120117
121- // ! set the cell volume variable in pelec
118+ // ! Set the cell volume variable in pelec
122119 this ->pelec ->omega = ucell.omega ;
123120
124- // ! 3) inititlize the charge density.
121+ // ! Inititlize the charge density.
125122 this ->chr .allocate (inp.nspin );
126123
127- // 3.5) initialize DFT-1/2
124+ // ! Initialize DFT-1/2
128125 if (PARAM.inp .dfthalf_type > 0 )
129126 {
130127 this ->vsep_cell = new VSep;
131128 this ->vsep_cell ->init_vsep (*this ->pw_rhod , ucell.sep_cell );
132129 }
133130
134- // ! 4) initialize the potential.
131+ // ! Initialize the potential.
135132 if (this ->pelec ->pot == nullptr )
136133 {
137134 this ->pelec ->pot = new elecstate::Potential (this ->pw_rhod ,
138135 this ->pw_rho , &ucell, &this ->locpp .vloc , &(this ->sf ),
139136 &(this ->solvent ), &(this ->pelec ->f_en .etxc ), &(this ->pelec ->f_en .vtxc ), this ->vsep_cell );
140137 }
141138
142- // ! 5) Initalize local pseudopotential
139+ // ! Initalize local pseudopotential
143140 this ->locpp .init_vloc (ucell, this ->pw_rhod );
144141 ModuleBase::GlobalFunc::DONE (GlobalV::ofs_running, " LOCAL POTENTIAL" );
145142
146- // ! 6) Initalize non-local pseudopotential
143+ // ! Initalize non-local pseudopotential
147144 this ->ppcell .init (ucell, &this ->sf , this ->pw_wfc );
148145 this ->ppcell .init_vnl (ucell, this ->pw_rhod );
149146 ModuleBase::GlobalFunc::DONE (GlobalV::ofs_running, " NON-LOCAL POTENTIAL" );
150147
151- // ! 7) Allocate and initialize psi
148+ // ! Allocate and initialize psi
152149 this ->p_psi_init = new psi::PSIInit<T, Device>(inp.init_wfc ,
153- inp.ks_solver ,
154- inp.basis_type ,
155- GlobalV::MY_RANK,
156- ucell,
157- this ->sf ,
158- this ->kv ,
159- this ->ppcell ,
160- *this ->pw_wfc );
150+ inp.ks_solver , inp.basis_type , GlobalV::MY_RANK, ucell,
151+ this ->sf , this ->kv , this ->ppcell , *this ->pw_wfc );
161152
162153 allocate_psi (this ->psi , this ->kv .get_nks (), this ->kv .ngk , PARAM.globalv .nbands_l , this ->pw_wfc ->npwk_max );
163154
@@ -169,7 +160,7 @@ void ESolver_KS_PW<T, Device>::before_all_runners(UnitCell& ucell, const Input_p
169160
170161 ModuleBase::GlobalFunc::DONE (GlobalV::ofs_running, " INIT BASIS" );
171162
172- // ! 8) setup occupations
163+ // ! Setup occupations
173164 if (inp.ocp )
174165 {
175166 elecstate::fixed_weights (inp.ocp_kb ,
@@ -180,7 +171,7 @@ void ESolver_KS_PW<T, Device>::before_all_runners(UnitCell& ucell, const Input_p
180171 this ->pelec ->skip_weights );
181172 }
182173
183- // 9) initialize exx pw
174+ // ! Initialize exx pw
184175 if (inp.calculation == " scf" || inp.calculation == " relax" || inp.calculation == " cell-relax"
185176 || inp.calculation == " md" )
186177 {
@@ -203,14 +194,10 @@ void ESolver_KS_PW<T, Device>::before_scf(UnitCell& ucell, const int istep)
203194 ModuleBase::TITLE (" ESolver_KS_PW" , " before_scf" );
204195 ModuleBase::timer::tick (" ESolver_KS_PW" , " before_scf" );
205196
206- // ----------------------------------------------------------
207- // ! 1) Call before_scf() of ESolver_KS
208- // ----------------------------------------------------------
197+ // ! Call before_scf() of ESolver_KS
209198 ESolver_KS<T, Device>::before_scf (ucell, istep);
210199
211- // ----------------------------------------------------------
212- // ! 2) Init variables (cell changed)
213- // ----------------------------------------------------------
200+ // ! Init variables (once the cell has changed)
214201 if (ucell.cell_parameter_updated )
215202 {
216203 this ->ppcell .rescale_vnl (ucell.omega );
@@ -225,35 +212,27 @@ void ESolver_KS_PW<T, Device>::before_scf(UnitCell& ucell, const int istep)
225212 this ->p_psi_init ->prepare_init (PARAM.inp .pw_seed );
226213 }
227214
228- // ----------------------------------------------------------
229- // ! 3) init Hamiltonian (cell changed)
230- // ----------------------------------------------------------
215+ // ! Init Hamiltonian (cell changed)
231216 // ! Operators in HamiltPW should be reallocated once cell changed
232217 // ! delete Hamilt if not first scf
233218 this ->deallocate_hamilt ();
234219
235- // allocate HamiltPW
220+ // ! Allocate HamiltPW
236221 this ->allocate_hamilt (ucell);
237222
238- // ----------------------------------------------------------
239- // 4) setup potentials (local, non-local, sc, +U, DFT-1/2)
240- // ----------------------------------------------------------
223+ // ! Setup potentials (local, non-local, sc, +U, DFT-1/2)
241224 pw::setup_pot (istep, ucell, this ->kv , this ->sf , this ->pelec , this ->Pgrid ,
242225 this ->chr , this ->locpp , this ->ppcell , this ->vsep_cell ,
243226 this ->kspw_psi , this ->p_hamilt , this ->pw_wfc , this ->pw_rhod , PARAM.inp );
244227
245- // ----------------------------------------------------------
246- // ! 5) Initialize wave functions
247- // ----------------------------------------------------------
228+ // ! Initialize wave functions
248229 if (!this ->already_initpsi )
249230 {
250231 this ->p_psi_init ->initialize_psi (this ->psi , this ->kspw_psi , this ->p_hamilt , GlobalV::ofs_running);
251232 this ->already_initpsi = true ;
252233 }
253234
254- // ----------------------------------------------------------
255- // ! 6) Exx calculations
256- // ----------------------------------------------------------
235+ // ! Exx calculations
257236 if (PARAM.inp .calculation == " scf" || PARAM.inp .calculation == " relax"
258237 || PARAM.inp .calculation == " cell-relax" || PARAM.inp .calculation == " md" )
259238 {
@@ -271,7 +250,7 @@ void ESolver_KS_PW<T, Device>::before_scf(UnitCell& ucell, const int istep)
271250template <typename T, typename Device>
272251void ESolver_KS_PW<T, Device>::iter_init(UnitCell& ucell, const int istep, const int iter)
273252{
274- // call iter_init() of ESolver_KS
253+ // Call iter_init() of ESolver_KS
275254 ESolver_KS<T, Device>::iter_init (ucell, istep, iter);
276255
277256 if (iter == 1 )
@@ -280,10 +259,7 @@ void ESolver_KS_PW<T, Device>::iter_init(UnitCell& ucell, const int istep, const
280259 this ->p_chgmix ->mixing_restart_step = PARAM.inp .scf_nmax + 1 ;
281260 }
282261
283- // ----------------------------------------------------------
284- // for mixing restart
285- // the details should move somewhere else, 2025-04-13
286- // ----------------------------------------------------------
262+ // For mixing restart
287263 if (iter == this ->p_chgmix ->mixing_restart_step && PARAM.inp .mixing_restart > 0.0 )
288264 {
289265 this ->p_chgmix ->init_mixing ();
@@ -322,16 +298,12 @@ void ESolver_KS_PW<T, Device>::iter_init(UnitCell& ucell, const int istep, const
322298 }
323299 }
324300
325- // ----------------------------------------------------------
326301 // mohan move harris functional to here, 2012-06-05
327302 // use 'rho(in)' and 'v_h and v_xc'(in)
328- // ----------------------------------------------------------
329303 this ->pelec ->f_en .deband_harris = this ->pelec ->cal_delta_eband (ucell);
330304
331- // ----------------------------------------------------------
332305 // update local occupations for DFT+U
333306 // should before lambda loop in DeltaSpin
334- // ----------------------------------------------------------
335307 if (PARAM.inp .dft_plus_u && (iter != 1 || istep != 0 ))
336308 {
337309 auto * dftu = ModuleDFTU::DFTU::get_instance ();
@@ -450,38 +422,28 @@ void ESolver_KS_PW<T, Device>::update_pot(UnitCell& ucell, const int istep, cons
450422template <typename T, typename Device>
451423void ESolver_KS_PW<T, Device>::iter_finish(UnitCell& ucell, const int istep, int & iter, bool & conv_esolver)
452424{
453- // ----------------------------------------------------------
454425 // Related to EXX
455- // ----------------------------------------------------------
456426 if (GlobalC::exx_info.info_global .cal_exx && !exx_helper.op_exx ->first_iter )
457427 {
458428 this ->pelec ->set_exx (exx_helper.cal_exx_energy (kspw_psi));
459429 }
460430
461- // ----------------------------------------------------------
462- // deband is calculated from "output" charge density calculated
463- // in sum_band
464- // need 'rho(out)' and 'vr (v_h(in) and v_xc(in))'
465- // ----------------------------------------------------------
431+ // deband is calculated from "output" charge density
466432 this ->pelec ->f_en .deband = this ->pelec ->cal_delta_eband (ucell);
467433
468- // ----------------------------------------------------------
469- // 1) Call iter_finish() of ESolver_KS
470- // ----------------------------------------------------------
434+ // Call iter_finish() of ESolver_KS
471435 ESolver_KS<T, Device>::iter_finish (ucell, istep, iter, conv_esolver);
472436
473- // ----------------------------------------------------------
474- // 2) Update USPP-related quantities
475437 // D in USPP needs vloc, thus needs update when veff updated
476438 // calculate the effective coefficient matrix for non-local
477439 // pp projectors, liuyu 2023-10-24
478- // ----------------------------------------------------------
479440 if (PARAM.globalv .use_uspp )
480441 {
481442 ModuleBase::matrix veff = this ->pelec ->pot ->get_effective_v ();
482443 this ->ppcell .cal_effective_D (veff, this ->pw_rhod , ucell);
483444 }
484445
446+ // Related to EXX
485447 if (GlobalC::exx_info.info_global .cal_exx )
486448 {
487449 if (GlobalC::exx_info.info_global .separate_loop )
@@ -524,24 +486,23 @@ void ESolver_KS_PW<T, Device>::iter_finish(UnitCell& ucell, const int istep, int
524486 }
525487 }
526488
527- // ----------------------------------------------------------
528- // 4) check if oscillate for delta_spin method
529- // ----------------------------------------------------------
489+ // check if oscillate for delta_spin method
530490 if (PARAM.inp .sc_mag_switch )
531491 {
532492 spinconstrain::SpinConstrain<std::complex <double >>& sc
533493 = spinconstrain::SpinConstrain<std::complex <double >>::getScInstance ();
534494 if (!sc.higher_mag_prec )
535495 {
536- sc.higher_mag_prec
537- = this -> p_chgmix -> if_scf_oscillate (iter, this ->drho , PARAM.inp .sc_os_ndim , PARAM.inp .scf_os_thr );
496+ sc.higher_mag_prec = this -> p_chgmix -> if_scf_oscillate (iter,
497+ this ->drho , PARAM.inp .sc_os_ndim , PARAM.inp .scf_os_thr );
538498 if (sc.higher_mag_prec )
539499 { // if oscillate, increase the precision of magnetization and do mixing_restart in next iteration
540500 this ->p_chgmix ->mixing_restart_step = iter + 1 ;
541501 }
542502 }
543503 }
544504
505+ // the output quantities
545506 ModuleIO::ctrl_iter_pw (istep, iter, conv_esolver, this ->psi ,
546507 this ->kv , this ->pw_wfc , PARAM.inp );
547508}
@@ -552,25 +513,26 @@ void ESolver_KS_PW<T, Device>::after_scf(UnitCell& ucell, const int istep, const
552513 ModuleBase::TITLE (" ESolver_KS_PW" , " after_scf" );
553514 ModuleBase::timer::tick (" ESolver_KS_PW" , " after_scf" );
554515
555- // since ESolver_KS::psi is hidden by ESolver_KS_PW::psi,
516+ // Since ESolver_KS::psi is hidden by ESolver_KS_PW::psi,
556517 // we need to copy the data from ESolver_KS::psi to ESolver_KS_PW::psi.
557- // This part needs to be removed when we have a better design.
558518 // sunliang 2025-04-10
559519 if (PARAM.inp .out_elf [0 ] > 0 )
560520 {
561521 this ->ESolver_KS <T, Device>::psi = new psi::Psi<T>(this ->psi [0 ]);
562522 }
563523
524+ // Call 'after_scf' of ESolver_KS
564525 ESolver_KS<T, Device>::after_scf (ucell, istep, conv_esolver);
565526
566- // transfer data from GPU to CPU in pw basis
527+ // Transfer data from GPU to CPU in pw basis
567528 if (this ->device == base_device::GpuDevice)
568529 {
569530 castmem_2d_d2h_op ()(this ->psi [0 ].get_pointer () - this ->psi [0 ].get_psi_bias (),
570531 this ->kspw_psi [0 ].get_pointer () - this ->kspw_psi [0 ].get_psi_bias (),
571532 this ->psi [0 ].size ());
572533 }
573534
535+ // Output quantities
574536 ModuleIO::ctrl_scf_pw<T, Device>(istep, ucell, this ->pelec , this ->chr , this ->kv , this ->pw_wfc ,
575537 this ->pw_rho , this ->pw_rhod , this ->pw_big , this ->psi , this ->kspw_psi ,
576538 this ->__kspw_psi , this ->ctx , this ->Pgrid , PARAM.inp );
0 commit comments