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 "esolver_fp.h"
8#include "source_psi/psi.h"
9
10namespace ModuleESolver
11{
12class ESolver_OF : public ESolver_FP
13{
14 public:
17
18 virtual void before_all_runners(UnitCell& ucell, const Input_para& inp) override;
19
20 virtual void runner(UnitCell& ucell, const int istep) override;
21
22 virtual void after_all_runners(UnitCell& ucell) override;
23
24 virtual double cal_energy() override;
25
26 virtual void cal_force(UnitCell& ucell, ModuleBase::matrix& force) override;
27
28 virtual void cal_stress(UnitCell& ucell, ModuleBase::matrix& stress) override;
29
30 private:
31 // ======================= variables ==========================
32 // ---------- the kinetic energy density functionals ----------
33 KEDF_Manager* kedf_manager_ = nullptr; // KEDF manager, which will be initialized in before_all_runners
34
35 // ----------------- the optimization methods ------------------
39 ModuleBase::Opt_CG* opt_cg_mag_ = nullptr; // for spin2 case, under testing
40
41 // ----------------- necessary parameters from INPUT ------------
42 std::string of_kinetic_ = "wt"; // Kinetic energy functional, such as TF, VW, WT
43 std::string of_method_ = "tn"; // optimization method, include cg1, cg2, tn (default), bfgs
44 std::string of_conv_ = "energy"; // select the convergence criterion, potential, energy (default), or both
45 double of_tole_ = 2e-6; // tolerance of the energy change (in Ry) for determining the convergence, default=2e-6 Ry
46 double of_tolp_ = 1e-5; // tolerance of potential for determining the convergence, default=1e-5 in a.u.
47 int max_iter_ = 50; // scf_nmax
48
49 // ------------------ parameters from other module --------------
50 double dV_ = 0; // volume of one grid point in real space
51 double* nelec_ = nullptr; // number of electrons with each spin
52
53 // ----- parameters and arrays used in density optimization -----
54 int iter_ = 0; // iteration number
55 double** pdirect_ = nullptr; // optimization direction of phi, which is sqrt(rho)
56 std::complex<double>** precip_dir_ = nullptr; // direction in reciprocal space, used when of_full_pw=false.
57 double* theta_ = nullptr; // step length
58 double** pdEdphi_ = nullptr; // dE/dphi
59 double** pdLdphi_ = nullptr; // dL/dphi
60 double** pphi_ = nullptr; // pphi[i] = ppsi.get_pointer(i), which will be freed in ~Psi().
61 char* task_ = nullptr; // used in line search
62 int tn_spin_flag_ = -1; // spin flag used in cal_potential, which will be called by opt_tn
63 int max_dcsrch_ = 200; // max no. of line search
64 int flag_ = -1; // flag of TN
65 Charge* ptemp_rho_ = nullptr; // used in line search
66 psi::Psi<double>* psi_ = nullptr; // sqrt(rho)
67
68 // ----------------- used for convergence check -------------------
69 double energy_llast_ = 0;
70 double energy_last_ = 0;
71 double energy_current_ = 0;
72 double normdLdphi_llast_ = 100;
73 double normdLdphi_last_ = 100;
74 double normdLdphi_ = 100.;
75
76 // ==================== main process of OFDFT ======================
77 void before_opt(const int istep, UnitCell& ucell);
78 void update_potential(UnitCell& ucell);
79 void optimize(UnitCell& ucell);
80 void update_rho();
81 bool check_exit(bool& conv_esolver);
82 void after_opt(const int istep, UnitCell& ucell, const bool conv_esolver);
83
84 // ============================ tools ===============================
85 // --------------------- initialize ---------------------------------
86 void init_elecstate(UnitCell& ucell);
87 void allocate_array();
88
89 // --------------------- calculate physical qualities ---------------
90 std::function<void(double*, double*)> bound_cal_potential_;
91 void cal_potential_wrapper(double* ptemp_phi, double* rdLdphi);
92 void cal_potential(double* ptemp_phi, double* rdLdphi, UnitCell& ucell);
93 void cal_dEdtheta(double** ptemp_phi, Charge* temp_rho, UnitCell& ucell, double* ptheta, double* rdEdtheta);
94 double cal_mu(double* pphi, double* pdEdphi, double nelec);
95
96 // --------------------- determine the optimization direction -------
97 void adjust_direction();
98 void check_direction(double* dEdtheta, double** ptemp_phi, UnitCell& ucell);
99 void test_direction(double* dEdtheta, double** ptemp_phi, UnitCell& ucell);
100
101 // --------------------- output the necessary information -----------
102 void print_info(const bool conv_esolver);
103
104 // --------------------- interface to blas --------------------------
105 double inner_product(double* pa, double* pb, int length, double dV = 1)
106 {
107 double innerproduct = BlasConnector::dot(length, pa, 1, pb, 1);
108 innerproduct *= dV;
109 return innerproduct;
110 }
111
112 // ---------------------- interfaces to optimization methods --------
113 void init_opt();
114 void get_direction(UnitCell& ucell);
115 void get_step_length(double* dEdtheta, double** ptemp_phi, UnitCell& ucell);
116};
117} // namespace ModuleESolver
118
119#endif
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:142
Definition charge.h:20
Definition kedf_manager.h:15
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:16
A class designed to deal with optimization problems with Truncated-Newton (TN) method....
Definition opt_TN.hpp:22
Definition matrix.h:19
double ** pphi_
Definition esolver_of.h:60
virtual void after_all_runners(UnitCell &ucell) override
Output the FINAL_ETOT.
Definition esolver_of.cpp:540
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:430
Charge * ptemp_rho_
Definition esolver_of.h:65
void optimize(UnitCell &ucell)
Get the optimization direction (this->pdirection_) and the step length (this->theta)
Definition esolver_of.cpp:355
double energy_last_
Definition esolver_of.h:70
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:208
void test_direction(double *dEdtheta, double **ptemp_phi, UnitCell &ucell)
ONLY used for test. Check the validity of KEDF.
Definition esolver_of_tool.cpp:356
double inner_product(double *pa, double *pb, int length, double dV=1)
Definition esolver_of.h:105
int flag_
Definition esolver_of.h:64
int max_dcsrch_
Definition esolver_of.h:63
double ** pdLdphi_
Definition esolver_of.h:59
double energy_current_
Definition esolver_of.h:71
psi::Psi< double > * psi_
Definition esolver_of.h:66
int max_iter_
Definition esolver_of.h:47
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:310
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:119
virtual void before_all_runners(UnitCell &ucell, const Input_para &inp) override
Initialize of the first-principels energy solver.
Definition esolver_of.cpp:58
void allocate_array()
Allocate the arrays, as well as this->psi_ and this->ptemp_rho_.
Definition esolver_of_tool.cpp:71
std::string of_method_
Definition esolver_of.h:43
KEDF_Manager * kedf_manager_
Definition esolver_of.h:33
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:484
double dV_
Definition esolver_of.h:50
double of_tolp_
Definition esolver_of.h:46
void get_direction(UnitCell &ucell)
[Interface to opt] Call optimization methods to get the optimization direction
Definition esolver_of_interface.cpp:61
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:211
virtual void cal_force(UnitCell &ucell, ModuleBase::matrix &force) override
Calculate the force.
Definition esolver_of.cpp:575
double * nelec_
Definition esolver_of.h:51
void init_opt()
[Interface to opt] Initialize the opts
Definition esolver_of_interface.cpp:12
std::string of_conv_
Definition esolver_of.h:44
void print_info(const bool conv_esolver)
Print nessecary information to the screen, and write the components of the total energy into running_...
Definition esolver_of_tool.cpp:395
ModuleBase::Opt_TN * opt_tn_
Definition esolver_of.h:37
virtual void runner(UnitCell &ucell, const int istep) override
run energy solver
Definition esolver_of.cpp:153
virtual void cal_stress(UnitCell &ucell, ModuleBase::matrix &stress) override
Calculate the stress.
Definition esolver_of.cpp:586
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:304
std::complex< double > ** precip_dir_
Definition esolver_of.h:56
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:177
double normdLdphi_last_
Definition esolver_of.h:73
virtual double cal_energy() override
Calculate the total energy. NOTE THIS FUNCTION SHOULD BE CALLEDD AFTER POTENTIAL HAS BEEN UPDATED.
Definition esolver_of.cpp:551
char * task_
Definition esolver_of.h:61
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:102
double normdLdphi_llast_
Definition esolver_of.h:72
ModuleBase::Opt_CG * opt_cg_mag_
Definition esolver_of.h:39
void cal_potential_wrapper(double *ptemp_phi, double *rdLdphi)
Definition esolver_of_interface.cpp:52
ModuleBase::Opt_DCsrch * opt_dcsrch_
Definition esolver_of.h:38
void adjust_direction()
Rotate and renormalize the direction |d>, make it orthogonal to phi (<d|phi> = 0),...
Definition esolver_of_tool.cpp:223
void init_elecstate(UnitCell &ucell)
Initialize this->pelec, as well as this->pelec->pot.
Definition esolver_of_tool.cpp:18
int tn_spin_flag_
Definition esolver_of.h:62
double of_tole_
Definition esolver_of.h:45
void update_rho()
Update the charge density and "wavefunction" (phi) after one step of optimization phi = cos(theta) * ...
Definition esolver_of.cpp:397
ModuleBase::Opt_CG * opt_cg_
Definition esolver_of.h:36
int iter_
Definition esolver_of.h:54
double ** pdEdphi_
Definition esolver_of.h:58
std::string of_kinetic_
Definition esolver_of.h:42
double energy_llast_
Definition esolver_of.h:69
double ** pdirect_
Definition esolver_of.h:55
double normdLdphi_
Definition esolver_of.h:74
std::function< void(double *, double *)> bound_cal_potential_
Definition esolver_of.h:90
double * theta_
Definition esolver_of.h:57
bool conv_esolver
Definition esolver.h:44
Definition unitcell.h:16
Definition psi.h:37
plane wave basis
Definition opt_test_tools.cpp:93
Definition input_parameter.h:12