26 this->
mach_prec_ = std::numeric_limits<double>::epsilon();
70 void (
T::*p_calGradient)(
72 double* rtemp_gradient)
91 innerproduct *= this->
dV_;
108 double dd = this->
inner_product(pcg_direction, pcg_direction, this->nx_);
110 epsilon = 2 * sqrt(this->mach_prec_) * (1 + sqrt(xx)) / sqrt(dd);
134 void (
T::*p_calGradient)(double* px, double* rgradient))
139 double* minus_gradient =
new double[this->
nx_];
140 double* temp_x =
new double[this->
nx_];
141 double* temp_gradient =
new double[this->
nx_];
142 double* cg_direct =
new double[this->
nx_];
143 double* temp_Hcgd =
new double[this->
nx_];
144 for (
int i = 0; i < this->
nx_; ++i)
146 minus_gradient[i] = -pgradient[i];
158 double cg_alpha = 0.;
159 double init_residual = 0.;
160 double last_residual = 0.;
161 double curr_residual = 0.;
171 for (
int i = 0; i < this->
nx_; ++i)
173 temp_x[i] = px[i] + epsilon * cg_direct[i];
175 (t->*p_calGradient)(temp_x, temp_gradient);
176 for (
int i = 0; i < this->
nx_; ++i)
178 temp_Hcgd[i] = (temp_gradient[i] - pgradient[i]) / epsilon;
185 for (
int i = 0; i < this->
nx_; ++i)
187 rdirect[i] += cg_alpha * cg_direct[i];
192 else if (cg_ifPD == -2)
198 for (
int i = 0; i < this->
nx_; ++i)
200 rdirect[i] += cg_alpha * cg_direct[i];
204 last_residual = curr_residual;
209 init_residual = curr_residual;
214 if (curr_residual < 0.1 * init_residual)
220 else if (cg_iter > 50)
225 else if ((fabs(curr_residual - last_residual) / curr_residual) < 0.01 && cg_iter > 9)
232 delete[] minus_gradient;
233 delete[] temp_gradient;
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
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
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
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
A class designed to deal with optimization problems with Truncated-Newton (TN) method....
Definition opt_TN.hpp:22
Opt_CG cg_
Definition opt_TN.hpp:81
double get_epsilon(double *px, double *pcg_direction)
Get epsilon used in interpolation. epsilon = 2*sqrt(mach_prec_) * (1+|x|) / |d|. || means modulu.
Definition opt_TN.hpp:103
void next_direct(double *px, double *pgradient, int &flag, double *rdirect, T *t, void(T::*p_calGradient)(double *ptemp_x, double *rtemp_gradient))
double dV_
Definition opt_TN.hpp:82
double inner_product(double *pa, double *pb, int length)
Definition opt_TN.hpp:88
int nx_
Definition opt_TN.hpp:84
~Opt_TN()
Definition opt_TN.hpp:28
void refresh(int nx_new=0)
Refresh the class. If nx changes, reallocate space in cg_.
Definition opt_TN.hpp:53
Opt_TN()
Definition opt_TN.hpp:24
int get_iter()
Definition opt_TN.hpp:75
double mach_prec_
Definition opt_TN.hpp:86
void allocate(int nx)
Allocate the space for the arrays in cg_.
Definition opt_TN.hpp:35
void set_para(double dV)
Definition opt_TN.hpp:41
int iter_
Definition opt_TN.hpp:85
#define T
Definition exp.cpp:237
void ZEROS(std::complex< T > *u, const TI n)
Definition global_function.h:109
Definition array_pool.h:6
void reduce_all(T &object)
reduce in all process
Definition depend_mock.cpp:14