Skip to content

Commit 1b2738f

Browse files
author
Fei Yang
committed
change BFGS name and make lattice_change_cg and ions_move_cg shorter
1 parent 07658be commit 1b2738f

File tree

6 files changed

+50
-106
lines changed

6 files changed

+50
-106
lines changed

source/source_io/read_input_item_relax.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void ReadInput::item_relax()
1212
item.annotation = "cg; bfgs; sd; cg; cg_bfgs;";
1313
read_sync_string(input.relax_method);
1414
item.check_value = [](const Input_Item& item, const Parameter& para) {
15-
const std::vector<std::string> relax_methods = {"cg", "bfgs", "sd", "cg_bfgs","bfgs_trad","lbfgs"};
15+
const std::vector<std::string> relax_methods = {"cg", "bfgs_old", "sd", "cg_bfgs","bfgs","lbfgs"};
1616
if (std::find(relax_methods.begin(),relax_methods.end(), para.input.relax_method)==relax_methods.end())
1717
{
1818
const std::string warningstr = nofound_str(relax_methods, "relax_method");

source/source_relax/ions_move_cg.cpp

Lines changed: 22 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "ions_move_cg.h"
2-
32
#include "ions_move_basic.h"
43
#include "source_base/global_function.h"
54
#include "source_base/global_variable.h"
@@ -77,20 +76,16 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
7776
// ncggrad is a parameter to control the cg method , every ten cg directions,
7877
// we change the direction back to the steepest descent method
7978
static int ncggrad = 0;
80-
8179
static double fa = 0.0;
8280
static double fb = 0.0;
8381
static double fc = 0.0;
84-
8582
static double xa = 0.0;
8683
static double xb = 0.0;
8784
static double xc = 0.0;
8885
static double xpt = 0.0;
8986
static double steplength = 0.0;
9087
static double fmax = 0.0;
91-
9288
static int nbrent = 0;
93-
9489

9590
// some arrays
9691
double *pos = new double[dim];
@@ -100,7 +95,6 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
10095
double *cg_grad = new double[dim];
10196
double best_x = 0.0;
10297
double fmin = 0.0;
103-
10498
int flag = 0;
10599

106100
ModuleBase::GlobalFunc::ZEROS(pos, dim);
@@ -137,7 +131,6 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
137131
{
138132
Ions_Move_Basic::check_converged(ucell, grad);
139133
}
140-
141134
if (Ions_Move_Basic::converged)
142135
{
143136
Ions_Move_Basic::terminate(ucell);
@@ -155,21 +148,7 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
155148
flag); // we use the last direction ,the last grad and the grad now to get the direction now
156149
ncggrad++;
157150

158-
double norm = 0.0;
159-
for (int i = 0; i < dim; ++i)
160-
{
161-
norm += pow(cg_grad[i], 2);
162-
}
163-
norm = sqrt(norm);
164-
165-
if (norm != 0.0)
166-
{
167-
for (int i = 0; i < dim; ++i)
168-
{
169-
cg_gradn[i] = cg_grad[i] / norm;
170-
}
171-
}
172-
151+
normalize(cg_gradn, cg_grad, dim);
173152
setup_move(move0, cg_gradn, steplength); // move the atom position
174153
Ions_Move_Basic::move_atoms(ucell, move0, pos);
175154

@@ -181,7 +160,6 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
181160

182161
f_cal(move0, move0, dim, xb); // xb = trial steplength
183162
f_cal(move0, grad, dim, fa); // fa is the projection force in this direction
184-
185163
fmax = fa;
186164
sd = false;
187165

@@ -204,7 +182,6 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
204182
double e1 = etot_in;
205183
f_cal(move0, grad, dim, fb);
206184
f_cal(move0, move0, dim, xb);
207-
208185
if ((std::abs(fb) < std::abs((fa) / 10.0)))
209186
{
210187
sd = true;
@@ -214,21 +191,7 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
214191
goto CG_begin;
215192
}
216193

217-
double norm = 0.0;
218-
for (int i = 0; i < dim; ++i)
219-
{
220-
norm += pow(cg_grad0[i], 2);
221-
}
222-
norm = sqrt(norm);
223-
224-
if (norm != 0.0)
225-
{
226-
for (int i = 0; i < dim; ++i)
227-
{
228-
cg_gradn[i] = cg_grad0[i] / norm;
229-
}
230-
}
231-
194+
normalize(cg_gradn, cg_grad, dim);
232195
third_order(e0, e1, fa, fb, xb, best_x); // cubic interpolation
233196

234197
if (best_x > 6 * xb || best_x < (-xb))
@@ -238,7 +201,6 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
238201

239202
setup_move(move, cg_gradn, best_x);
240203
Ions_Move_Basic::move_atoms(ucell, move, pos);
241-
242204
trial = false;
243205
xa = 0;
244206
f_cal(move0, move, dim, xc);
@@ -250,7 +212,6 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
250212
{
251213
double xtemp, ftemp;
252214
f_cal(move0, grad, dim, fc);
253-
254215
fmin = std::abs(fc);
255216
nbrent++;
256217

@@ -275,24 +236,9 @@ void Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
275236
goto CG_begin;
276237
}
277238

278-
double norm = 0.0;
279-
for (int i = 0; i < dim; ++i)
280-
{
281-
norm += pow(cg_grad0[i], 2);
282-
}
283-
norm = sqrt(norm);
284-
285-
if (norm != 0.0)
286-
{
287-
for (int i = 0; i < dim; ++i)
288-
{
289-
cg_gradn[i] = cg_grad0[i] / norm;
290-
}
291-
}
292-
239+
normalize(cg_gradn, cg_grad, dim);
293240
setup_move(move, cg_gradn, best_x);
294241
Ions_Move_Basic::move_atoms(ucell, move, pos);
295-
296242
Ions_Move_Basic::relax_bfgs_init = xc;
297243
}
298244
}
@@ -342,7 +288,6 @@ void Ions_Move_CG::setup_cg_grad(double *grad,
342288
cgp_gp += cg_grad0[i] * grad0[i];
343289
cgp_g += cg_grad0[i] * grad[i];
344290
}
345-
346291
assert(g_gp != 0.0);
347292
const double gamma1 = gg / gp_gp; // FR
348293
// const double gamma2 = -(gg - g_gp)/(cgp_g - cgp_gp); //CW
@@ -508,3 +453,22 @@ void Ions_Move_CG::setup_move(double *move, double *cg_gradn, const double &trus
508453
}
509454
return;
510455
}
456+
457+
void Ions_Move_CG::normalize(double *cg_gradn, const double *cg_grad, int dim)
458+
{
459+
double norm = 0.0;
460+
for (int i = 0; i < dim; ++i)
461+
{
462+
norm += pow(cg_grad[i], 2);
463+
}
464+
norm = sqrt(norm);
465+
466+
if (norm != 0.0)
467+
{
468+
for (int i = 0; i < dim; ++i)
469+
{
470+
cg_gradn[i] = cg_grad[i] / norm;
471+
}
472+
}
473+
return;
474+
}

source/source_relax/ions_move_cg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Ions_Move_CG
3838
const double &fb,
3939
const double x,
4040
double &best_x);
41+
void normalize(double *cg_gradn, const double *cg_grad, int dim);
4142
};
4243

4344
#endif

source/source_relax/ions_move_methods.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void Ions_Move_Methods::cal_movement(const int &istep,
6060
// Ions_Move_Basic::istep = istep;
6161
Ions_Move_Basic::istep = force_step;
6262

63-
if (Ions_Move_Basic::relax_method == "bfgs")
63+
if (Ions_Move_Basic::relax_method == "bfgs_old")
6464
{
6565
// move_ions
6666
// output tau
@@ -79,7 +79,7 @@ void Ions_Move_Methods::cal_movement(const int &istep,
7979
{
8080
cg.start(ucell, f, etot); // added by pengfei 13-8-10
8181
}
82-
else if(Ions_Move_Basic::relax_method == "bfgs_trad")
82+
else if(Ions_Move_Basic::relax_method == "bfgs")
8383
{
8484
bfgs_trad.relax_step(f,ucell);
8585
}

source/source_relax/lattice_change_cg.cpp

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -165,21 +165,7 @@ void Lattice_Change_CG::start(UnitCell &ucell, const ModuleBase::matrix &stress_
165165
flag); // we use the last direction ,the last grad and the grad now to get the direction now
166166
ncggrad++;
167167

168-
double norm = 0.0;
169-
for (int i = 0; i < dim; ++i)
170-
{
171-
norm += pow(cg_grad[i], 2);
172-
}
173-
norm = sqrt(norm);
174-
175-
if (norm != 0.0)
176-
{
177-
for (int i = 0; i < dim; ++i)
178-
{
179-
cg_gradn[i] = cg_grad[i] / norm;
180-
}
181-
}
182-
168+
normalize(cg_gradn, cg_grad, dim);
183169
setup_move(move0, cg_gradn, steplength); // move the atom position
184170
Lattice_Change_Basic::change_lattice(ucell, move0, lat);
185171

@@ -214,21 +200,7 @@ void Lattice_Change_CG::start(UnitCell &ucell, const ModuleBase::matrix &stress_
214200
goto CG_begin;
215201
}
216202

217-
double norm = 0.0;
218-
for (int i = 0; i < dim; ++i)
219-
{
220-
norm += pow(cg_grad0[i], 2);
221-
}
222-
norm = sqrt(norm);
223-
224-
if (norm != 0.0)
225-
{
226-
for (int i = 0; i < dim; ++i)
227-
{
228-
cg_gradn[i] = cg_grad0[i] / norm;
229-
}
230-
}
231-
203+
normalize(cg_gradn, cg_grad, dim);
232204
third_order(e0, e1, fa, fb, xb, best_x); // cubic interpolation
233205

234206
if (best_x > 6 * xb || best_x < (-xb))
@@ -279,21 +251,7 @@ void Lattice_Change_CG::start(UnitCell &ucell, const ModuleBase::matrix &stress_
279251
goto CG_begin;
280252
}
281253

282-
double norm = 0.0;
283-
for (int i = 0; i < dim; ++i)
284-
{
285-
norm += pow(cg_grad0[i], 2);
286-
}
287-
norm = sqrt(norm);
288-
289-
if (norm != 0.0)
290-
{
291-
for (int i = 0; i < dim; ++i)
292-
{
293-
cg_gradn[i] = cg_grad0[i] / norm;
294-
}
295-
}
296-
254+
normalize(cg_gradn, cg_grad, dim);
297255
setup_move(move, cg_gradn, best_x);
298256
Lattice_Change_Basic::change_lattice(ucell, move, lat);
299257

@@ -512,3 +470,22 @@ void Lattice_Change_CG::setup_move(double *move, double *cg_gradn, const double
512470
}
513471
return;
514472
}
473+
474+
void Lattice_Change_CG::normalize(double *cg_gradn, const double *cg_grad, int dim)
475+
{
476+
double norm = 0.0;
477+
for (int i = 0; i < dim; ++i)
478+
{
479+
norm += pow(cg_grad[i], 2);
480+
}
481+
norm = sqrt(norm);
482+
483+
if (norm != 0.0)
484+
{
485+
for (int i = 0; i < dim; ++i)
486+
{
487+
cg_gradn[i] = cg_grad[i] / norm;
488+
}
489+
}
490+
return;
491+
}

source/source_relax/lattice_change_cg.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class Lattice_Change_CG
4040
const double &fb,
4141
const double x,
4242
double &best_x);
43+
44+
void normalize(double *cg_gradn, const double *cg_grad, int dim);
4345
};
4446

4547
#endif

0 commit comments

Comments
 (0)