Skip to content

Commit ff4b003

Browse files
author
abacus_fixer
committed
refactor(esolver): extract init_esolver factory from base class file
The factory functions init_esolver() and determine_type() were previously hosted in esolver.cpp, which is nominally the implementation file of the abstract base class ESolver. To instantiate the concrete derived classes (ESolver_KS_PW, ESolver_KS_LCAO, ESolver_OF, etc.), esolver.cpp had to #include 13 derived-class headers, making the base-class file reverse- depend on all its derivatives. This violates ABACUS coding rule 3 ("Keep header dependencies minimal") and the dependency-inversion principle. Fix by moving the factory code into a dedicated esolver_factory module so that esolver.h/cpp only carries the ESolver interface and knows nothing about derived classes. The factory is the single place allowed to see both the interface and all concrete implementations, which is its essential responsibility. Changes: - Add source/source_esolver/esolver_factory.h with declarations of determine_type() and init_esolver(); forward-declares Input_para and ESolver to keep includes minimal. - Add source/source_esolver/esolver_factory.cpp containing the full bodies of determine_type() and init_esolver() moved verbatim from esolver.cpp, plus the 13 derived-class includes that now live here. - Remove source/source_esolver/esolver.cpp (its entire content was the factory code; the ESolver base class has no out-of-line members). - Trim source/source_esolver/esolver.h: drop determine_type() and init_esolver() declarations and their doxygen blocks. The Input_para forward declaration is retained because before_all_runners() takes it as a parameter. - Update source/source_esolver/CMakeLists.txt and source/Makefile.Objects to compile esolver_factory.cpp instead of esolver.cpp. - Add explicit #include "source_esolver/esolver_factory.h" in source/source_main/driver_run.cpp, the sole caller of init_esolver(); previously the declaration was pulled in transitively via relax_driver.h. No runtime behavior change. After this refactor, touching a derived esolver header no longer forces recompilation of the base-class file. Verified: incremental build of `esolver` and `abacus_std_para` targets in build_std_para (ENABLE_LCAO=ON) passes.
1 parent cead755 commit ff4b003

6 files changed

Lines changed: 43 additions & 22 deletions

File tree

source/Makefile.Objects

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ OBJS_ELECSTAT_LCAO=elecstate_lcao.o\
270270
cal_dm_psi.o\
271271
cal_edm_tddft.o\
272272

273-
OBJS_ESOLVER=esolver.o\
273+
OBJS_ESOLVER=esolver_factory.o\
274274
esolver_ks.o\
275275
esolver_fp.o\
276276
esolver_ks_pw.o\

source/source_esolver/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
list(APPEND objects
2-
esolver.cpp
2+
esolver_factory.cpp
33
esolver_ks.cpp
44
esolver_fp.cpp
55
esolver_ks_pw.cpp

source/source_esolver/esolver.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,6 @@ class ESolver
5151
std::string classname;
5252
};
5353

54-
/**
55-
* @brief A subrutine called in init_esolver()
56-
* This function returns type of ESolver
57-
* Based on PARAM.inp.basis_type and PARAM.inp.esolver_type
58-
* @return [out] std::string The type of ESolver
59-
*/
60-
std::string determine_type();
61-
62-
/**
63-
* @brief Determine and initialize an ESolver based on input information.
64-
*
65-
* This function determines the type of ESolver to create based on input information and initializes
66-
* the corresponding ESolver child class. It supports various ESolver types including ksdft_pw,
67-
* ksdft_lcao, ksdft_lcao_tddft, sdft_pw, ofdft, lj_pot, and dp_pot.
68-
*
69-
* @return [out] A pointer to an ESolver object that will be initialized.
70-
*/
71-
ESolver* init_esolver(const Input_para& inp);
72-
7354
} // namespace ModuleESolver
7455

7556
#endif
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
#include "esolver.h"
1+
#include "esolver_factory.h"
22

3+
#include "esolver.h"
34
#include "esolver_ks_pw.h"
45
#include "esolver_sdft_pw.h"
56
#include "source_base/module_device/device.h"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef ESOLVER_FACTORY_H
2+
#define ESOLVER_FACTORY_H
3+
4+
#include <string>
5+
6+
struct Input_para;
7+
8+
namespace ModuleESolver
9+
{
10+
11+
class ESolver;
12+
13+
/**
14+
* @brief Determine the ESolver type string from input parameters.
15+
*
16+
* The type is decided based on PARAM.inp.basis_type and PARAM.inp.esolver_type,
17+
* together with device/precision hints written to the running log.
18+
*
19+
* @return [out] std::string The type label consumed by init_esolver().
20+
*/
21+
std::string determine_type();
22+
23+
/**
24+
* @brief Determine and initialize an ESolver based on input information.
25+
*
26+
* This function determines the type of ESolver to create based on input
27+
* information and initializes the corresponding ESolver child class. It
28+
* supports various ESolver types including ksdft_pw, ksdft_lcao,
29+
* ksdft_lcao_tddft, sdft_pw, ofdft, lj_pot, and dp_pot.
30+
*
31+
* @param [in] inp Input parameters used to select and configure the ESolver.
32+
* @return [out] A pointer to the newly created ESolver object.
33+
*/
34+
ESolver* init_esolver(const Input_para& inp);
35+
36+
} // namespace ModuleESolver
37+
38+
#endif

source/source_main/driver_run.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "source_cell/check_atomic_stru.h"
33
#include "source_cell/module_neighbor/sltk_atom_arrange.h"
44
#include "source_relax/relax_driver.h"
5+
#include "source_esolver/esolver_factory.h"
56
#include "source_io/module_parameter/parameter.h"
67
#include "source_io/module_json/para_json.h"
78
#include "source_io/module_output/print_info.h"

0 commit comments

Comments
 (0)