ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
opt_tn.h
Go to the documentation of this file.
1#ifndef OPT_TN_H
2#define OPT_TN_H
3
4#include "opt_cg.h"
5
6#include <limits>
7
8namespace ModuleBase
9{
21class Opt_TN
22{
23 public:
24 Opt_TN();
25 ~Opt_TN() {};
26
32 void allocate(int nx);
33
34 void set_para(double dV);
35
42 void refresh(int nx_new = 0);
43
44 template <class T>
45 void next_direct(
46 double* px, // current x
47 double* pgradient, // df(x)/dx
48 int& flag, // record which truncated condition was triggered, 0 for cond.1, 1 for cond.2, and 2 for cond.3
49 double* rdirect, // next optimization direction
50 T* t, // point of class T, which contains the gradient function
51 void (T::*p_calGradient)(
52 double* ptemp_x,
53 double* rtemp_gradient) // a function point, which calculates the gradient at provided x
54 );
55
57 {
58 return this->iter_;
59 }
60
61 private:
63 double dV_ = 1.;
64
65 int nx_ = 0; // length of the solution array x
66 int iter_ = 0; // number of the iteration
67 double mach_prec_ = 0.; // machine precision
68
69 double inner_product(double* pa, double* pb, int length);
70
79 double get_epsilon(double* px, double* pcg_direction);
80};
81
93template <class T>
94void Opt_TN::next_direct(double* px,
95 double* pgradient,
96 int& flag,
97 double* rdirect,
98 T* t,
99 void (T::*p_calGradient)(double* px, double* rgradient))
100{
101 // initialize arrays and parameters
102 ModuleBase::GlobalFunc::ZEROS(rdirect, this->nx_); // very important
103
104 double* minus_gradient = new double[this->nx_]; // b=-g, which will be used in CG
105 double* temp_x = new double[this->nx_]; // temp_x = x + step * cg_direct, used in interpolation
106 double* temp_gradient = new double[this->nx_]; // df(temp_x)/dx
107 double* cg_direct = new double[this->nx_]; // rdirect += cg_alpha * cg_direct at each step
108 double* temp_Hcgd = new double[this->nx_]; // Hessian * cg_direct
109 for (int i = 0; i < this->nx_; ++i)
110 {
111 minus_gradient[i] = -pgradient[i];
112 }
113 ModuleBase::GlobalFunc::ZEROS(cg_direct, this->nx_);
114 ModuleBase::GlobalFunc::ZEROS(temp_x, this->nx_);
115 ModuleBase::GlobalFunc::ZEROS(temp_gradient, this->nx_);
116 ModuleBase::GlobalFunc::ZEROS(temp_Hcgd, this->nx_);
117
118 cg_.refresh(0, minus_gradient);
119 int cg_iter = 0;
120 int cg_ifPD = 0;
121
122 double epsilon = 0.; // step length in interpolation
123 double cg_alpha = 0.; // step length got by CG
124 double init_residual = 0.; // initial residual of CG
125 double last_residual = 0.; // last residual of CG
126 double curr_residual = 0.; // current residual of CG
127
128 while (true)
129 {
130 cg_.next_direct(temp_Hcgd, 0, cg_direct);
131
132 // get temp_Hcgd with interpolation
133 // Hcgd = (df(temp_x)/dx - df(x)/x) / epsilon, where temp_x = x + step * cg_direct
134 epsilon = this->get_epsilon(px, cg_direct);
135 // epsilon = 1e-9;
136 for (int i = 0; i < this->nx_; ++i)
137 {
138 temp_x[i] = px[i] + epsilon * cg_direct[i];
139 }
140 (t->*p_calGradient)(temp_x, temp_gradient);
141 for (int i = 0; i < this->nx_; ++i)
142 {
143 temp_Hcgd[i] = (temp_gradient[i] - pgradient[i]) / epsilon;
144 }
145
146 // get CG step length and update rdirect
147 cg_alpha = cg_.step_length(temp_Hcgd, cg_direct, cg_ifPD);
148 if (cg_ifPD == -1) // Hessian is not positive definite, and cgiter = 1.
149 {
150 for (int i = 0; i < this->nx_; ++i)
151 {
152 rdirect[i] += cg_alpha * cg_direct[i];
153 }
154 flag = -1;
155 break;
156 }
157 else if (cg_ifPD == -2) // Hessian is not positive definite, and cgiter > 1.
158 {
159 flag = -2;
160 break;
161 }
162
163 for (int i = 0; i < this->nx_; ++i)
164 {
165 rdirect[i] += cg_alpha * cg_direct[i];
166 }
167
168 // store residuals used in truncated conditions
169 last_residual = curr_residual;
170 curr_residual = cg_.get_residual();
171 cg_iter = cg_.get_iter();
172 if (cg_iter == 1)
173 {
174 init_residual = curr_residual;
175 }
176
177 // check truncated conditions
178 // if (curr_residual < 1e-12)
179 if (curr_residual < 0.1 * init_residual)
180 {
181 flag = 0;
182 // std::cout << "cg_ iter_ = " << cg_iter << "\n";
183 break;
184 }
185 else if (cg_iter > 50)
186 {
187 flag = 1;
188 break;
189 }
190 else if ((fabs(curr_residual - last_residual) / curr_residual) < 0.01 && cg_iter > 9)
191 {
192 flag = 2;
193 break;
194 }
195 }
196 this->iter_++;
197 delete[] minus_gradient;
198 delete[] temp_gradient;
199 delete[] temp_x;
200 delete[] temp_Hcgd;
201 delete[] cg_direct;
202}
203} // namespace ModuleBase
204#endif
A class designed to deal with optimization problems with CG method. Three forms of CG methods have be...
Definition opt_cg.h:25
Opt_CG cg_
Definition opt_tn.h:62
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.cpp:40
void next_direct(double *px, double *pgradient, int &flag, double *rdirect, T *t, void(T::*p_calGradient)(double *ptemp_x, double *rtemp_gradient))
Get the next direction d with TN method.
Definition opt_tn.h:94
double dV_
Definition opt_tn.h:63
double inner_product(double *pa, double *pb, int length)
Definition opt_tn.cpp:33
int nx_
Definition opt_tn.h:65
~Opt_TN()
Definition opt_tn.h:25
void refresh(int nx_new=0)
Refresh the class. If nx changes, reallocate space in cg_.
Definition opt_tn.cpp:23
Opt_TN()
Definition opt_tn.cpp:6
int get_iter()
Definition opt_tn.h:56
double mach_prec_
Definition opt_tn.h:67
void allocate(int nx)
Allocate the space for the arrays in cg_.
Definition opt_tn.cpp:11
void set_para(double dV)
Definition opt_tn.cpp:17
int iter_
Definition opt_tn.h:66
#define T
Definition exp.cpp:237
void ZEROS(std::complex< T > *u, const TI n)
Definition global_function.h:109
Definition clebsch_gordan_coeff.cpp:8
constexpr double epsilon
Definition vector3.h:13