ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
esolver_of.h
Go to the documentation of this file.
1#ifndef ESOLVER_OF_H
2#define ESOLVER_OF_H
3
4#include <string>
5#include <vector>
6
7#include "esolver_fp.h"
10#include "source_psi/psi.h"
12
13namespace ModuleESolver
14{
15class ESolver_OF : public ESolver_FP
16{
17 public:
20
21 virtual void before_all_runners(BaseCell &basecell, const Input_para &inp) override;
22
23 virtual void runner(BaseCell &basecell, const int istep) override;
24
25 virtual void after_all_runners(BaseCell &basecell) override;
26
27 virtual double cal_energy() override;
28
29 virtual void cal_force(BaseCell &basecell, ModuleBase::matrix &force) override;
30
31 virtual void cal_stress(BaseCell &basecell, ModuleBase::matrix &stress) override;
32
33 protected:
34 // ======================= variables ==========================
35 // ---------- the kinetic energy density functionals ----------
36 KEDF_Manager *kedf_manager_ = nullptr; // KEDF manager, which will be initialized in before_all_runners
37
38 // ----------------- the optimization methods ------------------
42 ModuleBase::Opt_CG *opt_cg_mag_ = nullptr; // for spin2 case, under testing
43
44 // ----------------- necessary parameters from INPUT ------------
45 std::string of_kinetic_ = "wt"; // Kinetic energy functional, such as TF, VW, WT
46 std::string of_method_ = "tn"; // optimization method, include cg1, cg2, tn (default), bfgs
47 std::string of_conv_ = "energy"; // select the convergence criterion, potential, energy (default), or both
48 double of_tole_ = 2e-6; // tolerance of the energy change (in Ry) for determining the convergence, default=2e-6 Ry
49 double of_tolp_ = 1e-5; // tolerance of potential for determining the convergence, default=1e-5 in a.u.
50 int max_iter_ = 50; // scf_nmax
51
52 // ------------------ parameters from other module --------------
53 double dV_ = 0; // volume of one grid point in real space
54 std::vector<double> nelec_; // number of electrons with each spin
55
56 // ----- parameters and arrays used in density optimization -----
57 int iter_ = 0; // iteration number
58 std::vector<double> pdirect_storage_;
59 std::vector<double *> pdirect_; // row views; optimization direction of phi, which is sqrt(rho)
60 std::vector<std::complex<double>> precip_dir_storage_;
61 std::vector<std::complex<double> *> precip_dir_; // row views; used when of_full_pw=false.
62 std::vector<double> theta_; // step length
63 std::vector<double> pdEdphi_storage_;
64 std::vector<double *> pdEdphi_; // row views; dE/dphi
65 std::vector<double> pdLdphi_storage_;
66 std::vector<double *> pdLdphi_; // row views; dL/dphi
67 double **pphi_ = nullptr; // pphi[i] = ppsi.get_pointer(i), which will be freed in ~Psi().
68 std::string task_; // used in line search
69 int tn_spin_flag_ = -1; // spin flag used in cal_potential, which will be called by opt_tn
70 int max_dcsrch_ = 200; // max no. of line search
71 int flag_ = -1; // flag of TN
72 Charge *ptemp_rho_ = nullptr; // used in line search
73 psi::Psi<double> *psi_ = nullptr; // sqrt(rho)
74
75 // ----------------- used for convergence check -------------------
76 double energy_llast_ = 0;
77 double energy_last_ = 0;
78 double energy_current_ = 0;
79 double normdLdphi_llast_ = 100;
80 double normdLdphi_last_ = 100;
81 double normdLdphi_ = 100.;
82
83 // ==================== main process of OFDFT ======================
84 void before_opt(const int istep, UnitCell &ucell);
85 void update_potential(UnitCell &ucell);
86 void optimize(UnitCell &ucell);
87 void update_rho();
88 bool check_exit(bool &conv_esolver);
89 void after_opt(const int istep, UnitCell &ucell, const bool conv_esolver);
90
91 // ============================ tools ===============================
92 // --------------------- initialize ---------------------------------
93 void init_elecstate(UnitCell &ucell);
94 void allocate_array();
95 void resize_spin_arrays();
96
97 // --------------------- calculate physical qualities ---------------
98 std::function<void(double *, double *)> bound_cal_potential_;
99 void cal_potential_wrapper(double *ptemp_phi, double *rdLdphi);
100 void cal_potential(double *ptemp_phi, double *rdLdphi, UnitCell &ucell);
101 void cal_dEdtheta(
102 double **ptemp_phi,
103 Charge *temp_rho,
104 UnitCell &ucell,
105 double *ptheta,
106 double *rdEdtheta);
107 double cal_mu(double *pphi, double *pdEdphi, double nelec);
108
109 // --------------------- determine the optimization direction -------
110 void adjust_direction();
111 void check_direction(double *dEdtheta, double **ptemp_phi, UnitCell &ucell);
112 void test_direction(double *dEdtheta, double **ptemp_phi, UnitCell &ucell);
113
114 // --------------------- interface to blas --------------------------
116 double *pa,
117 double *pb,
118 int length,
119 double dV = 1) const
120 {
121 double innerproduct = BlasConnector::dot(length, pa, 1, pb, 1);
122 innerproduct *= dV;
123 return innerproduct;
124 }
125
126 // ---------------------- interfaces to optimization methods --------
127 void init_opt();
128 void get_direction(UnitCell &ucell);
129 void get_step_length(double *dEdtheta, double **ptemp_phi, UnitCell &ucell);
130};
131} // namespace ModuleESolver
132
133#endif
Definition basecell.h:9
static float dot(const int n, const float *const X, const int incX, const float *const Y, const int incY, base_device::AbacusDevice_t device_type=base_device::AbacusDevice_t::CpuDevice)
Definition blas_connector_vector.cpp:143
Definition charge.h:24
Definition kedf_manager.h:16
A class designed to deal with optimization problems with CG method. Three forms of CG methods have be...
Definition opt_cg.h:25
A interface to line search.
Definition opt_dcsrch.h:17
A class designed to deal with optimization problems with Truncated-Newton (TN) method....
Definition opt_tn.h:22
Definition matrix.h:15
Definition esolver_fp.h:33
Definition opt_test_tools.h:19
std::vector< double * > pdLdphi_
Definition esolver_of.h:66
double ** pphi_
Definition esolver_of.h:67
bool check_exit(bool &conv_esolver)
Check convergence, return ture if converge or iter >= max_iter_, and print the necessary information.
Definition esolver_of.cpp:375
Charge * ptemp_rho_
Definition esolver_of.h:72
std::string task_
Definition esolver_of.h:68
void optimize(UnitCell &ucell)
Get the optimization direction (this->pdirection_) and the step length (this->theta).
Definition esolver_of.cpp:319
virtual void cal_stress(BaseCell &basecell, ModuleBase::matrix &stress) override
Calculate the stress.
Definition esolver_of.cpp:542
double inner_product(double *pa, double *pb, int length, double dV=1) const
Definition esolver_of.h:115
double energy_last_
Definition esolver_of.h:77
void before_opt(const int istep, UnitCell &ucell)
Prepare to optimize the charge density, update elecstate, kedf, and opts if needed calculate ewald en...
Definition esolver_of.cpp:184
std::vector< double > pdEdphi_storage_
Definition esolver_of.h:63
void test_direction(double *dEdtheta, double **ptemp_phi, UnitCell &ucell)
ONLY used for test. Check the validity of KEDF.
Definition esolver_of_tool.cpp:359
int flag_
Definition esolver_of.h:71
int max_dcsrch_
Definition esolver_of.h:70
std::vector< std::complex< double > * > precip_dir_
Definition esolver_of.h:61
double energy_current_
Definition esolver_of.h:78
std::vector< std::complex< double > > precip_dir_storage_
Definition esolver_of.h:60
psi::Psi< double > * psi_
Definition esolver_of.h:73
int max_iter_
Definition esolver_of.h:50
void update_potential(UnitCell &ucell)
Get dL/dphi = dL/drho * drho/dphi = (dE/drho - mu) * 2 * phi, as well as normdLdphi = sqrt{<dL/dphi|d...
Definition esolver_of.cpp:275
void cal_potential(double *ptemp_phi, double *rdLdphi, UnitCell &ucell)
Get dL/dphi = dL/drho * drho/dphi = (dE/drho - mu) * 2 * ptemp_phi and store it in rdLdphi.
Definition esolver_of_tool.cpp:131
std::vector< double > theta_
Definition esolver_of.h:62
virtual void before_all_runners(BaseCell &basecell, const Input_para &inp) override
Initialize of the first-principels energy solver.
Definition esolver_of.cpp:41
void allocate_array()
Allocate the arrays, as well as this->psi_ and this->ptemp_rho_.
Definition esolver_of_tool.cpp:72
std::string of_method_
Definition esolver_of.h:46
KEDF_Manager * kedf_manager_
Definition esolver_of.h:36
void after_opt(const int istep, UnitCell &ucell, const bool conv_esolver)
After optimization, output the charge density, effective potential, ..., if needed.
Definition esolver_of.cpp:431
double dV_
Definition esolver_of.h:53
double of_tolp_
Definition esolver_of.h:49
void get_direction(UnitCell &ucell)
[Interface to opt] Call optimization methods to get the optimization direction
Definition esolver_of_interface.cpp:60
std::vector< double * > pdEdphi_
Definition esolver_of.h:64
double cal_mu(double *pphi, double *pdEdphi, double nelec)
Calculate the chemical potential mu. mu = <dE/dphi|phi> / (2 * nelec).
Definition esolver_of_tool.cpp:216
void init_opt()
[Interface to opt] Initialize the opts
Definition esolver_of_interface.cpp:11
std::string of_conv_
Definition esolver_of.h:47
ModuleBase::Opt_TN * opt_tn_
Definition esolver_of.h:40
std::function< void(double *, double *)> bound_cal_potential_
Definition esolver_of.h:98
virtual void after_all_runners(BaseCell &basecell) override
Output the FINAL_ETOT.
Definition esolver_of.cpp:485
void check_direction(double *dEdtheta, double **ptemp_phi, UnitCell &ucell)
Make sure that dEdtheta<0 at theta = 0, preparing to call the line search.
Definition esolver_of_tool.cpp:309
void cal_dEdtheta(double **ptemp_phi, Charge *temp_rho, UnitCell &ucell, double *ptheta, double *rdEdtheta)
Calculate dE/dTheta and store it in rdEdtheta. dE/dTheta = <dE / dtemp_phi | dtemp_phi / dTheta> = <d...
Definition esolver_of_tool.cpp:180
double normdLdphi_last_
Definition esolver_of.h:80
virtual double cal_energy() override
Calculate the total energy. NOTE THIS FUNCTION SHOULD BE CALLEDD AFTER POTENTIAL HAS BEEN UPDATED.
Definition esolver_of.cpp:499
void get_step_length(double *dEdtheta, double **ptemp_phi, UnitCell &ucell)
[Interface to opt] Call line search to find the best step length
Definition esolver_of_interface.cpp:98
double normdLdphi_llast_
Definition esolver_of.h:79
std::vector< double > nelec_
Definition esolver_of.h:54
ModuleBase::Opt_CG * opt_cg_mag_
Definition esolver_of.h:42
std::vector< double * > pdirect_
Definition esolver_of.h:59
void cal_potential_wrapper(double *ptemp_phi, double *rdLdphi)
Definition esolver_of_interface.cpp:51
void resize_spin_arrays()
Definition esolver_of_tool.cpp:100
std::vector< double > pdirect_storage_
Definition esolver_of.h:58
ModuleBase::Opt_DCsrch * opt_dcsrch_
Definition esolver_of.h:41
void adjust_direction()
Rotate and renormalize the direction |d>, make it orthogonal to phi (<d|phi> = 0),...
Definition esolver_of_tool.cpp:228
void init_elecstate(UnitCell &ucell)
Initialize this->pelec, as well as this->pelec->pot.
Definition esolver_of_tool.cpp:22
int tn_spin_flag_
Definition esolver_of.h:69
double of_tole_
Definition esolver_of.h:48
virtual void runner(BaseCell &basecell, const int istep) override
run energy solver
Definition esolver_of.cpp:131
virtual void cal_force(BaseCell &basecell, ModuleBase::matrix &force) override
Calculate the force.
Definition esolver_of.cpp:522
std::vector< double > pdLdphi_storage_
Definition esolver_of.h:65
void update_rho()
Update the charge density and "wavefunction" (phi) after one step of optimization phi = cos(theta) * ...
Definition esolver_of.cpp:355
ModuleBase::Opt_CG * opt_cg_
Definition esolver_of.h:39
int iter_
Definition esolver_of.h:57
std::string of_kinetic_
Definition esolver_of.h:45
double energy_llast_
Definition esolver_of.h:76
double normdLdphi_
Definition esolver_of.h:81
bool conv_esolver
Definition esolver.h:46
Provide the basic information about unitcell.
Definition unitcell.h:16
Definition psi.h:37
The First-Principles (FP) Energy Solver Class.
Definition opt_test_tools.cpp:93
Definition input_parameter.h:13