ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
opt_CG.h
Go to the documentation of this file.
1#ifndef OPT_CG_H
2#define OPT_CG_H
3
4#include <math.h>
5
6#include <iostream>
7
8#include "global_function.h"
10
11namespace ModuleBase
12{
24class Opt_CG
25{
26 public:
27 Opt_CG();
28 ~Opt_CG();
29
30 void init_b(double* pinp_b // b in the linear equation Ax = b
31 );
32 void allocate(int nx // length of the solution array x
33 );
34 void set_para(double dV);
35 void refresh(int nx_new = 0, // length of new x, default 0 means the length doesn't change
36 double* pinp_b = nullptr // new b in Ax = b, default nullptr means we are dealing with general case
37 );
38
39 void next_direct(double* pgradient, // Ad for linear equaiont Ax=b, and gradient for general case
40 int label, // 0 for solve Ax=b, 1 for PR form, 2 for HZ form
41 double* rdirect // next direct
42 );
43 double step_length(double* pAd, // Ad for Ax=b
44 double* pdirect, // direct
45 int& ifPD // if postive definit
46 );
47
48 double get_residual()
49 {
50 return sqrt(this->gg_);
51 };
53 {
54 return this->iter_;
55 }
56
57 // void ZEROS(double *x, int n)
58 // {
59 // for (int i = 0; i < n; ++i) x[i] =0;
60 // }
61
62 private:
63 double dV_ = 1.;
64 int nx_ = 0; // length of the sulotion array x
65 int iter_ = 0; // number of iteration
66 double gg_ = 1000; // gradient dot gradient
67 double beta_ = 0.; // d = -g + beta * d
68 double eta_ = 0.01; // a constand used in HZ form
69 double* pdirect_old_ = nullptr; // direction of last step
70 double* pgradient_old_ = nullptr; // gradient, for meth=0, gradient is minus residual r.
71
72 // only for standard CG
73 double alpha_ = 0.; // step length in standard CG
74 double* pb_ = nullptr; // b in Ax=b, only for standard CG
75
76 void stantard_CGdirect(double* pAd, // Ad for Ax=b
77 double* rdirect // next direct
78 );
79 void PR_beta(double* pgradient // df(x)/dx
80 );
81 void HZ_beta(double* pgradient // df(x)/dx
82 );
83 double inner_product(double* pa, double* pb, int length)
84 {
85 double innerproduct = BlasConnector::dot(length, pa, 1, pb, 1);
86 innerproduct *= this->dV_;
87 return innerproduct;
88 }
89};
90} // namespace ModuleBase
91#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
A class designed to deal with optimization problems with CG method. Three forms of CG methods have be...
Definition opt_CG.h:25
void allocate(int nx)
Allocate the space for pdirect_old and pgradient_old.
Definition opt_CG.cpp:36
double get_residual()
Definition opt_CG.h:48
void init_b(double *pinp_b)
Initialize b before solving Ax = b.
Definition opt_CG.cpp:22
int iter_
Definition opt_CG.h:65
double beta_
Definition opt_CG.h:67
double eta_
Definition opt_CG.h:68
int nx_
Definition opt_CG.h:64
void PR_beta(double *pgradient)
Get the beta in PR form. beta_k = max{0, <g_k, g_k-g_{k-1}>/<g_{k-1}, g_{k-1}>} <> means inner produc...
Definition opt_CG.cpp:202
int get_iter()
Definition opt_CG.h:52
void next_direct(double *pgradient, int label, double *rdirect)
Get the next optimization direction.
Definition opt_CG.cpp:85
double alpha_
Definition opt_CG.h:73
void refresh(int nx_new=0, double *pinp_b=nullptr)
Refresh the class. If nx changes, reallocate space. If b is provided, initialize it.
Definition opt_CG.cpp:59
double * pdirect_old_
Definition opt_CG.h:69
~Opt_CG()
Definition opt_CG.cpp:10
double inner_product(double *pa, double *pb, int length)
Definition opt_CG.h:83
double gg_
Definition opt_CG.h:66
void HZ_beta(double *pgradient)
Get the beta in HZ form. See formula in Hager W W, Zhang H. SIAM Journal on optimization,...
Definition opt_CG.cpp:222
double * pb_
Definition opt_CG.h:74
double dV_
Definition opt_CG.h:63
Opt_CG()
Definition opt_CG.cpp:6
double * pgradient_old_
Definition opt_CG.h:70
void stantard_CGdirect(double *pAd, double *rdirect)
Get the next optimization direction with standard CG workflow.
Definition opt_CG.cpp:163
void set_para(double dV)
Definition opt_CG.cpp:47
double step_length(double *pAd, double *pdirect, int &ifPD)
Get the step length, only work for standard CG.
Definition opt_CG.cpp:131
Definition array_pool.h:6