Skip to content

We remove the template of ESolver_KS !!!#7015

Merged
mohanchen merged 14 commits intodeepmodeling:developfrom
mohanchen:remove_template3
Mar 13, 2026
Merged

We remove the template of ESolver_KS !!!#7015
mohanchen merged 14 commits intodeepmodeling:developfrom
mohanchen:remove_template3

Conversation

@mohanchen
Copy link
Collaborator

We remove the template of ESolver_KS !!!

abacus_fixer added 13 commits March 10, 2026 08:19
…rho::symmetrize_rho

- Add static method symmetrize_rho() in Symmetry_rho class
- Replace 7 duplicate code blocks with single function call
- Simplify code from 35 lines to 7 lines (80% reduction)
- Improve code readability and maintainability

Modified files:
- source_estate/module_charge/symmetry_rho.h: add static method declaration
- source_estate/module_charge/symmetry_rho.cpp: implement static method
- source_esolver/esolver_ks_lcao.cpp: 2 calls updated
- source_esolver/esolver_ks_pw.cpp: 1 call updated
- source_esolver/esolver_ks_lcao_tddft.cpp: 1 call updated
- source_esolver/esolver_ks_lcaopw.cpp: 1 call updated
- source_esolver/esolver_of.cpp: 1 call updated
- source_esolver/esolver_sdft_pw.cpp: 1 call updated

This refactoring follows the ESolver cleanup principle:
keep ESolver focused on high-level workflow control.
…dule

- Create new files deltaspin_lcao.h/cpp in module_deltaspin
- Extract DeltaSpin lambda loop logic from ESolver_KS_LCAO
- Simplify code from 18 lines to 1 line in hamilt2rho_single
- Separate LCAO and PW implementations for DeltaSpin

Modified files:
- source_esolver/esolver_ks_lcao.cpp: replace inline code with function call
- source_lcao/module_deltaspin/CMakeLists.txt: add new source file

New files:
- source_lcao/module_deltaspin/deltaspin_lcao.h: function declaration
- source_lcao/module_deltaspin/deltaspin_lcao.cpp: function implementation

This refactoring follows the ESolver cleanup principle:
keep ESolver focused on high-level workflow control.
- Add init_deltaspin_lcao() function for DeltaSpin initialization
- Add cal_mi_lcao_wrapper() function for magnetic moment calculation
- Refactor all DeltaSpin-related code in esolver_ks_lcao.cpp
- Simplify code from 29 lines to 3 lines (90% reduction)

Modified files:
- source_esolver/esolver_ks_lcao.cpp: replace 3 code blocks with function calls
- source_lcao/module_deltaspin/deltaspin_lcao.h: add 2 new function declarations
- source_lcao/module_deltaspin/deltaspin_lcao.cpp: implement 2 new functions

This completes the DeltaSpin refactoring for LCAO method:
1. init_deltaspin_lcao() - initialize DeltaSpin calculation
2. cal_mi_lcao_wrapper() - calculate magnetic moments
3. run_deltaspin_lambda_loop_lcao() - run lambda loop optimization

All functions follow the ESolver cleanup principle:
keep ESolver focused on high-level workflow control.
- Create new files dftu_lcao.h/cpp in source_lcao directory
- Add init_dftu_lcao() function for DFT+U initialization
- Add finish_dftu_lcao() function for DFT+U finalization
- Simplify code from 32 lines to 2 lines in esolver_ks_lcao.cpp
- Remove conditional checks from ESolver, move them to functions

Modified files:
- source_esolver/esolver_ks_lcao.cpp: replace 2 code blocks with function calls
- source_lcao/CMakeLists.txt: add new source file

New files:
- source_lcao/dftu_lcao.h: function declarations
- source_lcao/dftu_lcao.cpp: function implementations

This refactoring prepares for unifying old and new DFT+U implementations:
- Old DFT+U: source_lcao/module_dftu/
- New DFT+U: source_lcao/module_operator_lcao/op_dftu_lcao.cpp

All functions follow ESolver cleanup principle:
keep ESolver focused on high-level workflow control.
…r module

- Create new files diago_params.h/cpp in source_hsolver directory
- Add setup_diago_params_pw() function for PW diagonalization parameters
- Simplify code from 11 lines to 1 line in esolver_ks_pw.cpp
- Encapsulate diagonalization parameter setup logic

Modified files:
- source_esolver/esolver_ks_pw.cpp: replace inline code with function call
- source_hsolver/CMakeLists.txt: add new source file

New files:
- source_hsolver/diago_params.h: function declaration
- source_hsolver/diago_params.cpp: function implementation

This refactoring follows ESolver cleanup principle:
keep ESolver focused on high-level workflow control.
- Add Input_para parameter to cal_mi_lcao_wrapper function
- Add sc_mag_switch check to avoid calling cal_mi_lcao when DeltaSpin is disabled
- Fix 'atomCounts is not set' error in non-DeltaSpin calculations
- Update function call in esolver_ks_lcao.cpp

This fix resolves the CI/CD failure caused by commit 2a520e3.
The root cause was that cal_mi_lcao_wrapper was called without checking
sc_mag_switch, leading to uninitialized atomCounts error.

Modified files:
- source_esolver/esolver_ks_lcao.cpp: update function call
- source_lcao/module_deltaspin/deltaspin_lcao.h: add parameter
- source_lcao/module_deltaspin/deltaspin_lcao.cpp: add check

This follows the refactoring principle: preserve original condition checks
when extracting code to wrapper functions.
- Add #ifdef __LCAO conditional compilation in init_deltaspin_lcao and cal_mi_lcao_wrapper
- Fix parameter order in init_sc call for LCAO and non-LCAO builds
- Fix undefined reference to cal_mi_lcao in non-LCAO build

This fix resolves CI/CD compilation errors in both build_5pt (with __LCAO) and build_1p (without __LCAO) environments.

The The root cause was 1. init_sc has different parameter order in LCAO vs non-LCAO builds
   - LCAO: psi, dm, pelec
   - non-LCAO: psi, pelec
  2. cal_mi_lcao is only defined in LCAO build

Modified files:
- source_hsolver/diago_params.h: add setup_diago_params_sdft declaration
- source_lcao/module_deltaspin/deltaspin_lcao.cpp: add conditional compilation

This follows the refactoring principle: handle conditional compilation properly
when code has different implementations for different build configurations.
- Add setup_diago_params_sdft() function for SDFT diagonalization parameters
- Simplify code from 11 lines to 1 line in esolver_sdft_pw.cpp
- Encapsulate diagonalization parameter setup logic for SDFT

Modified files:
- source_esolver/esolver_sdft_pw.cpp: replace inline code with function call
- source_hsolver/diago_params.cpp: add setup_diago_params_sdft implementation

This refactoring follows ESolver cleanup principle:
keep ESolver focused on high-level workflow control.

Note: SDFT has different parameter setup logic compared to PW:
- Different need_subspace condition
- No SCF_ITER setting
- Always set PW_DIAG_NMAX (no nscf check)
- Create HamiltBase as a non-template base class for Hamilt<T, Device>
- Modify Hamilt<T, Device> to inherit from HamiltBase
- Change ESolver_KS::p_hamilt type from Hamilt<T, Device>* to HamiltBase*
- Add static_cast where needed when passing p_hamilt to functions expecting Hamilt<T, Device>*

This is the first step towards removing template parameters from ESolver.

Modified files:
- source/source_esolver/esolver_ks.h
- source/source_esolver/esolver_ks_lcaopw.cpp
- source/source_esolver/esolver_ks_pw.cpp
- source/source_esolver/esolver_sdft_pw.cpp
- source/source_hamilt/hamilt.h

New files:
- source/source_hamilt/hamilt_base.h
- Add static_cast<hamilt::Hamilt<T>*> when passing p_hamilt to functions
  expecting Hamilt<T, Device>* type
- Split long cast statements into multiple lines for better readability
- Files modified:
  - esolver_ks_pw.cpp: setup_pot, stp.init calls
  - esolver_ks_lcao.cpp: init_chg_hr, hsolver_lcao_obj.solve calls
  - esolver_ks_lcao_tddft.cpp: solve_psi, cal_edm_tddft, matrix calls
  - esolver_gets.cpp: ops access, output_SR call

This follows the HamiltBase refactoring strategy where p_hamilt is stored
as HamiltBase* and cast to Hamilt<T, Device>* when needed.
Move psi::Psi<T>* psi from ESolver_KS base class to derived classes
to eliminate template parameter dependency and improve code organization.

Changes:
1. ESolver_KS base class:
   - Remove psi::Psi<T>* psi member variable
   - Remove Setup_Psi<T>::deallocate_psi() call in destructor
   - Remove unnecessary includes: psi.h and setup_psi.h

2. ESolver_KS_LCAO:
   - Add psi::Psi<TK>* psi member variable
   - Add Setup_Psi<TK>::deallocate_psi() in destructor
   - Add include: setup_psi.h

3. ESolver_KS_LCAO_TDDFT:
   - Improve psi_laststep deallocation with nullptr check
   - psi member inherited from ESolver_KS_LCAO

4. ESolver_KS_PW:
   - Use stp.psi_cpu directly instead of base class psi
   - Remove unnecessary memory allocation in after_scf()

5. pw_others.cpp (BUG FIX):
   - Fix gen_bessel: use *(this->stp.psi_cpu) instead of this->psi[0]
   - Previous code accessed uninitialized base class psi (nullptr)
   - This was a latent bug that could cause crashes

Benefits:
- Eliminates template parameter T dependency in ESolver_KS base class
- Clearer memory management: each derived class manages its own psi
- Reduces compilation dependencies
- Fixes potential memory access bug in pw_others.cpp

Tested: Compiled successfully in build_5pt and build_1p
This is a major milestone in ESolver refactoring!

ESolver_KS no longer needs template parameters because:
- All member variables are non-template types
- All member functions do not use T or Device parameters
- Template parameters were only needed for derived classes

Changes:
1. ESolver_KS base class:
   - Remove template <typename T, typename Device> declaration
   - Remove all template declarations from member functions
   - Remove template instantiation code at end of file
   - Fix Tab indentation to spaces for better readability

2. Derived classes:
   - ESolver_KS_PW: public ESolver_KS (was ESolver_KS<T, Device>)
   - ESolver_KS_LCAO: public ESolver_KS (was ESolver_KS<TK>)
   - ESolver_GetS: public ESolver_KS (was ESolver_KS<std::complex<double>>)
   - Update base class calls: ESolver_KS:: (was ESolver_KS<T, Device>::)

Code reduction:
- esolver_ks.h: 78 -> 77 lines (-1 line)
- esolver_ks.cpp: 346 -> 317 lines (-29 lines)
- Total ESolver code: 424 -> 394 lines (-30 lines)
- Overall: 8 files changed, 50 insertions(+), 80 deletions(-), net -30 lines

Benefits:
- Simpler base class without template complexity
- Faster compilation (no template instantiation needed)
- Clearer inheritance hierarchy
- Easier to extract common code in future refactoring
- Sets foundation for further ESolver template removal

Tested: Compiled successfully in build_5pt
@mohanchen mohanchen requested a review from AsTonyshment March 11, 2026 22:37
@mohanchen mohanchen added Bugs Bugs that only solvable with sufficient knowledge of DFT Refactor Refactor ABACUS codes The Absolute Zero Reduce the "entropy" of the code to 0 labels Mar 11, 2026
Copy link
Collaborator

@AsTonyshment AsTonyshment left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mohanchen mohanchen merged commit 19f8df0 into deepmodeling:develop Mar 13, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bugs Bugs that only solvable with sufficient knowledge of DFT Refactor Refactor ABACUS codes The Absolute Zero Reduce the "entropy" of the code to 0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants